I don`t know where i do wrong.I cant send data from child to parent. What is wrong here? How can i grab the state from child and send to parent state?
this is the child component
import React from 'react';
export class Child extends React.Component{
constructor(props) {
super(props);
this.state= {
counter2: 5
}
}
render() {
return(
<div>
<button onClick={this.props.data}>Click me</button><span>{this.state.counter2}</span>
</div>
);
}
}
export default Child;
and i want to update the state in parent component
import React from 'react';
import {Child} from './Child';
export default class Parent extends React.Component{
constructor(props){
super(props);
this.state= {
counter: 0
}
}
update(){
this.setState({
counter: this.props.state.counter2
});
}
render(){
return(
<div>
<span>{this.state.counter}</span>
<Child data={this.update.bind(this)}/>
</div>
);
}
}
but i have a error: × TypeError: Cannot read property 'counter' of undefined?
i cant understand what i do wrong!
Thank you