Skip to content

Commit 078af5e

Browse files
committed
Create Parent and child components
1 parent 028501b commit 078af5e

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ I've branched the repo for individual topics.
77
To checkout all branches run: `git branch --all`.
88
To checkout individual branch run: `git checkout <branch name>`. From there on you should be good.
99

10-
## Branch Name: 01-react-app-webpack
10+
## Branch Name: 02-hierarchical-components
1111
## Branch Description:
12-
Set up React Application with using Webpack and Babel
12+
Demo for Parent Child Components
1313

1414
## Installation
1515

src/App.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import React from 'react';
2+
import ParentComponent from "./ParentComponent";
23

34
class App extends React.Component {
45
render() {
56
return (
6-
<div>My App Component</div>
7+
<div>
8+
My App Component
9+
<ParentComponent/>
10+
</div>
711
);
812
}
913
}

src/ChildComponent.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
3+
export const ChildComponent = ( props ) => {
4+
return (
5+
<h2>This is Child Component. Props are passed from { props.name } Component</h2>
6+
)
7+
}

src/ParentComponent.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from 'react';
2+
import {ChildComponent} from "./ChildComponent";
3+
4+
class ParentComponent extends React.Component {
5+
6+
/**
7+
* Overriding parent Constructor
8+
* @param props
9+
*/
10+
constructor( props ) {
11+
/**
12+
* Super is a way to call the Parent Constructor.
13+
* Pass props to super, When you want to access this.props in constructor, else this will be undefined
14+
* Passing or not passing props to super has no effect on later uses of this.props outside constructor.
15+
*/
16+
super( props );
17+
this.state = {
18+
name: 'Parent'
19+
}
20+
}
21+
22+
render() {
23+
return (
24+
<div>
25+
This is { this.state.name }
26+
<ChildComponent name={this.state.name}/>
27+
</div>
28+
);
29+
}
30+
}
31+
32+
export default ParentComponent;

0 commit comments

Comments
 (0)