Update props on child component with FlatList and Navigation - react-native

I am using functional components and have implemented the following workflow using React Native. I have a parent component that retrieves the current location via a custom hook (using the useState and useEffect hooks) and has registered watchPositionAsync. The location is updated on the parent component. On the parent component, I use a FlatList with onPress that navigates to the next screen as a child component with some props. One of them is the "updated" location.
When I click on the list item, the current location is sent to the child component.
Now the location changes by the listener, but the updated location will only be sent to the parent component and not to the child component. How do I have to use hooks to register the updated location at the child?
The implementation is a bit like on http://5.9.10.113/67799305/how-to-add-clickable-flatlist-item-and-navigate-to-detail-component-in-react-nat
just with a dynamic url from the home component as a prop of Detail.

you can use the redux or the context api. here is the simple solution has already been answered here.

Related

How to get states of multiple rendered component in react native?

I am developing a mobile app with react native. I have a component "Period" which has a couple of TextInputs, each TextInput is stored as state. This component gets rendered in a flatlist multiple times. Now I wonder how i can get the state values of each instance of "Period" that gets rendered in Flatlist?
Any Explanation how this works in React Native. I thought every component instance has its on states?
Thanks alot
I would suggest to put all your inputs states and onchange functions callbacks in the parent component containing the Flatlist, then you can pass your states as props to children components "Period"s.
I would suggest also working with a library such as Formik to handle dynamic forms with multiple inputs

Best lifecycle component to use when using react-native with redux

I'm trying to update a component after receiving props from redux using componentWillReceiveProps. On the next instance that props are sent to the component by redux componentWillReceiveProps does not fire a second time. Any idea why this is happening and the best lifecycle component to use in such a situation?.

When redux rerenders a view containing a StackNavigator it reset itself to initial screen

I am using react-navigation:
If i change the login property of the redux state when redux rerenders the View my StackLogin resets to initial state (If i am in the SignIn view it goes back to Login). Its like its not maintaining its own state. Maybe its something im missing about this library.
Regards
When the login prop changes, React will re-render your component. To determine what to render, the render() method is executed. Your render() method creates a new StackNavigator every time it is run. Therefore your <StackLogin /> is effectively reset when the prop changes.
What you probably want to do is to define StackLogin outside of the Login class. You certainly want to do it outside of the render() method.

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.

In React-Native, how can I call popToTop() for each NavigatorIOS tab within TabBarIOS?

The app I am building has a root client object that affects all of the subsequent views of the app. I want the user to be able to change clients and have all of the tabs reset, i.e. popToTop() and update the client appropriately.
I have a TabBarIOS component with 4 tabs, each tab is a NavigatorIOS component that manages the subsequent ListView components. How can I force all the NavigatorIOS components to popToTop() and re-render based on a client change?
Thanks in advance.
One simple approach would be to use an event emitter. Create an event emitter and pass it down to the component that owns the NavigatorIOS components. The owner can use the ref prop of each NavigatorIOS component to get a reference to each navigator.
The owner then can add a listener to the event emitter, and call popToTop() on each navigator when the listener is invoked. Then, it's just a matter of emitting the event when appropriate.