How can push back to parent screen and passprops?
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
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!!!