This project was bootstrapped with Create React App.
- Sending a prop from parent to child, in the parent component:
render() {
return (
<div>
<ChildComponent prop=val name='Jonathan' .../>
</div>
)
}
- Receiving a prop from the parent, in the child component:
constructor(props) {
super(props)
...
}
...
render() {
<div>
Name: {this.props.name}
</div>
}
- The rule when you're passing a function as a prop:
- Whenever you pass a function as a prop, you need to bind it!
constructor() {
super()
this.functionToPassAsProp = this.functionToPassAsProp.bind(this)
}
render() {
<div>
<ChildComponent someFn={this.functionToPassAsProp}>
</div>
}