react-native-navigation icon indicating copy to clipboard operation
react-native-navigation copied to clipboard

How can push back to parent screen and passprops?

Open peterlau0010 opened this issue 7 years ago • 3 comments

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

peterlau0010 avatar Oct 21 '19 06:10 peterlau0010

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

NISUSIN avatar Oct 21 '19 13:10 NISUSIN

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.

peresbri avatar Oct 22 '19 02:10 peresbri

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

peterlau0010 avatar Oct 23 '19 01:10 peterlau0010