Search code examples
react-nativereact-native-router-flux

How to change State in other component same level with parameter?


I have ParentComponent

render(){ 
  return('<'Child1Component/> '<'Child2Component/>)
}

So, I'm in Child2Component how to change State in Child1Componet with parameter. It is possible?


Solution

  • You can call one method of parent from child1 , and this method will call another method of child2 using refs

    add this to your parent component

    onPressSuccess = params => {
    this.refs.ComponentTwo.componentTwoMethod(params);
    };
    render() {
    return (
      <View>
        <ComponentOne onPressSuccess={this.onPressSuccess}> ... </ComponentOne>
        <ComponentTwo ref={"ComponentTwo"}> ... </ComponentTwo>
      </View>
    );
    }
    

    In <ComponentOne> call this.props.onPressSuccess(params) where params is parameters you want pass along with method.

    here componentTwoMethod is a method in <ComponentTwo>, which can take params and set state or whatever you want to do.