What is the better way send props between screen in react native? - react-native

I have to send data another screen. And I need to change the data on the parent screen when data changes on this screen. I tried to set/get params react-native navigation. But it doesn't work for me. What is the better way pass props/state between screen? How can I find out that my data has changed? and how can i store them for next time?

If you want to pass data from Screen A to Screen B or from a Parent component to a child component you should use props. This is what you are already doing just pass an extra prop which will be a callback function and is defined in the parent component which will update data in parent when some data changes in child component.
If you want to pass data to lets say nth screen (i.e from Screen A -> B -> C -> D ). Its better to use React Context.
You can read about it here
https://reactjs.org/docs/context.html

Related

React native modal screen change

I have a full screen modal and I want to "navigate" inside modal.
The structure of the modal
is a header and a footer that is the same in all screens and the body that is the different screens. Body is the only that will change.
How can achieve this with React Native Functional component without dependencies.
You can create :
A state React.useState step initialise at 0
An array of object with what you want for "body" elements
A function next and / or back to update state step change body
You can get elements of your body like so :
steps[activeStep].label
You have a good example from here demo sandbox demo and here documentation material ui stepper

How to call function inside child (tab) screen from parent screen in react native navigation

So i have a function inside a tab navigator screen, and I want to call from the parent of the tabs.the structure will be like this
And I need to call a function inside Tab 2 from Tabber Screen, how can I achieve this ?
All that I got from any example is accesing parent prop from child using screenProps. nothing is like how I need.
And for information, the parent screen using StackNavigator and the tabs using TabNavigator
Thanks for any help.

How to update state of nested components when app receive notifications?

So in my app I have a main component which have switch navigator and then inside switch navigator I have a tab navigator which renders different screens. I have registered notification handler in top component.
Now the problem is that when notification is received, I want to make some changes on screens inside tab navigator.
I have read about redux which will provide common store so state could be updated but I dont want to use that.
I have also read about lifting state up but in my case i have more nested components so I dont want to do that as well.
So suggest me some way to do it?
I have solved the problem by using react context api. It is simple wrap your top level component with provider and define some veriable and access that variable in any component.

React Native rendering of componentDidMount

How to pass props from one page to another without using navigation props?
Values can be passed by navigation props but without that how to do so?
This is a very broad question, but I will attempt an answer. The answer of course is, it depends. If you do not want to use navigation, then I recommend you lift up the state. In other words, place the the props to want to pass between screens in the state of a component that is the parent of both screens. The parent component can set the props of its children based on the state. You can use callback props on the children to set the parent components' state.
Another way to lift up the state and share data between components is to use a global state manager such as Redux. This allows you to have any component set the global state and have any component take in portions of the global state as props.
Either of these options will allow you to pass data between components.

React Navigation and Redux: goBack() doesn't refresh my props

This is a React Native project that includes React Navigation and Redux.
I have two scenes, the first one contains a ListView and a Button to add a new element to the list. This buttons navigates to a new Scene where I fill up a little form to create a new record that will appear on the list of the first scene.
When hitting the save button and going back to the first scene (list view), mapStateToProps reflects the change but no other life cycle method is called (componentWillReceiveProps, shouldComponentUpdate, etc), none of them.
Now, both Scenes use connect from redux and I'm going back from Scene 2 to Scene 1 with the goBack() method from React Navigation. I'm wondering if redux get's "disconnected" from a view when showing a new one and how can I connect it back? Again, the mapStateToProps from the ListView scene is showing the data updated but it seems no props are being mapped.
It just seem the props don't refresh if the redux state value hasn't really change. Copying the list array before modifying it and then assigning it to the payload seems to fix the issue.