React-native-navigation: How can push back to parent screen and passprops?

Created on 21 Oct 2019  路  3Comments  路  Source: wix/react-native-navigation

How can I push back to parent screen with parameters?

Navigation.pop(this.props.componentId)
can only close the current screen, but not passing values.

    Navigation.push(this.props.componentId, {
      component: {
        name: 'ParentView',
        passProps: {
          data: 'values',
        }
      },
    });

can pass values, but create a new ParentView...

Is that any way I can pass back value to parentView, and close current view? Thanks.

Environment

  • React Native Navigation version: 2.27.9
  • React Native version: 0.61.2
  • Platform(s) (iOS, Android, or both?): IOS
  • Device info (Simulator/Device? OS version? Debug/Release?): Simulator iPhone 11 pro, 13.1

Most helpful comment

Props should be immutable.
That said, in instances where you need to pass some data to previous screens, a ui store which act a bit as a controller is usually a good way to go. Whether you use redux or MobX or ..., having a connected UI store should apply changes instantly to your mounted screen.

All 3 comments

You can use this workaround:
Props are immutable but if you pass function, it can be updated.

Example:
ScreenA to ScreenB:

Navigation.push(this.props.componentId, {
    component: {
        name: 'ScreenB',
        passProps: {
            thisPro: ((this) => {return this})
        }
    }
})

On ScreenB, make your update and when you pop on your component, make this:

Navigation.pop(this.props.componentId);
componentWillUnmount() {
    //It will update parent's state
    this.props.thisPro.setState({data: "values"});
}

Back to your ScreenA, you can access new values with this.state.data

Props should be immutable.
That said, in instances where you need to pass some data to previous screens, a ui store which act a bit as a controller is usually a good way to go. Whether you use redux or MobX or ..., having a connected UI store should apply changes instantly to your mounted screen.

Thanks @peresbri and @NISUSIN , it work for me!!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

switchtrue picture switchtrue  路  3Comments

yayanartha picture yayanartha  路  3Comments

no23reason picture no23reason  路  3Comments

edcs picture edcs  路  3Comments

nbolender picture nbolender  路  3Comments