eve t from child to parent react code example
Example 1: communication child to parent react js
import React from 'react';
import Child from './Child'
export default class Parent extends React.Component {
constructor(props){
super(props);
this.state ={ text:''};
}
handleChangeInParent = (event) => {
this.setState({text:event.target.value})
}
render() {
return (
<>
<p>You have written: </p>
<p>{this.state.text}</p>
<Child handleChange={this.handleChangeInParent}/>
</>
)
}
}
Example 2: pass element from child to parent react
Parent:
<div className="col-sm-9">
<SelectLanguage onSelectLanguage={this.handleLanguage} />
</div>
Child:
handleLangChange = () => {
var lang = this.dropdown.value;
this.props.onSelectLanguage(lang);
}