I'm working with ReactJS with ES6, but I have some problems to communicate child > parent through props. Example of my approach:
class SearchBar extends React.Component {
handler(e){
this.props.filterUser(e.target.value);
}
render () {
return <div>
<input type='text' className='from-control search-bar' placeholder='Search' onChange={this.handler} />
</div>
}
}
export default class User extends React.Component {
constructor(props) {
super(props);
this.state = {name: '', age: '', filter: ''};
}
filterUser(filterValue){
this.setState({
filter: filterValue
});
}
render() {
return <div>
<SearchBar filterUser={this.filterUser} />
<span>Value: {this.state.filter}</span>
</div>
}
}
This returns Uncaught TypeError: this.props.filterUser is not a function
.
Any idea? Binding maybe?
[EDIT] Solution (Thanks @knowbody & @Felipe Skinner):
I was missing binding in my constructor. Binding in the SearchBar constructor works perfectly.
Using React.createClass()
(ES5), it automatically does bindings to this
for your functions. In ES6 you need bind this
manually. More info https://facebook.github.io/react/docs/reusable-components.html#es6-classes