react passing state as props

Solutions on MaxInterview for react passing state as props by the best coders in the world

showing results for - "react passing state as props"
Irene
05 Feb 2016
1REACT passing State as a prop to child component
2
3class MyApp extends React.Component {
4  constructor(props) {
5    super(props);
6    this.state = {
7      name: 'Tony Stark'
8    }
9  }
10  render() {
11    return (
12       <div>
13      	/* Pass State as a prop by adding a property and giving it a value from this.state */
14         <ChildComponent name={this.state.name}/>
15       </div>
16    );
17  }
18};
19
20class ChildComponent extends React.Component {
21  constructor(props) {
22    super(props);
23  }
24  render() {
25    return (
26    <div>
27      /* The prop of name is now accessed with this.props */
28      <h1>Hello, my name is: {this.props.name}</h1>
29    </div>
30    );
31  }
32};