//Main.js:
export default class Main extends React.Component {
constructor(props){
super(props)
this.state={
employee : {
employeeName: '',
//otherData
}
}
}
function handleEmployeeData(data) {
this.setState({
employee: data
})
}
render(){
return(
<Employee handleEmployeeData={this.handleEmployeeData} employee={this.state.employee}/>
<EmployeeAddress handleEmployeeData={this.handleEmployeeData} employee={this.state.employee}/>
);
}
//Employee.js or EmployeeAddress.js
Here set new employee data when onChange
or onClick
happens, this will set the employee data in parent state which both children can use
inside these files' render method you can do something like:
<NameInput* onBlur={e => this.props.handleEmployeeData({...this.props.employee, name: e.target.value})} />
You can do something like this, for any field in employeeData
- Notice
NameInput
is a placeholder you can replace it with any component you want
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…