I have a parent component and 2 children. Each child is an input field and the second input depends on the selection from the first input field. Is it possible in react to call a function insight sibling 2, only IF a function in Parent was executed? In my case: Child1 provides ID, parent uses ID, Child2 should make http request with same ID.
Below in my code (I left out a lot just so I don't have too much (unnecessary code) so if I missed something pls let me know) inside Parent,the "updateId" method received its argument from child1, ONLY when this happened (id was provided), only then I want to call the "getData" method in Sibling. I was thinking I could create boolean inside Parent set to true, when id is given, pass it to Sibling, calling the "getData" method only when its true but as I understand, that wouldn't be possible in react?! How could I do that?
PS - I am intentionally setting state in my children components as I was trying to keep the inputs (child=input) independent which can easy be exchanged.
export default class Parent extends React.Component {
constructor(props) {
super(props)
this.state = {
id: ""
}
this.updateId=this.updateId.bind(this);
}
updateId (id){
this.setState({
id:id
})
}
render() {
return (
<div>
<Child onChange={this.updateId} />
<Sibling id={this.state.id}/>
</div>
)
}
};
import apicall from :/....jsx;
export default class Sibling extends React.Component {
constructor(props) {
super(props)
this.state = {
siblings: []
}
this.getData=this.getData.bind(this);
}
getData (this.props.id){
apiCall(this.props.id).then(response=> {
this.setState({
siblings:response
})
}
render() {
return ...
}
};
export default class Child extends React.Component {
...
get ID
...
call this.props.onChange(selected.value)
};