How to get states of multiple rendered component in react native? - 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

Related

React native flatlist footer

I am new to react native. How can I pass some data to flat list's footer component? I tried extraData property of flat list but it is not working.
There are different ways to do this:
you can make your footer component a sub-component of your other component and use the state of the parent component to modify the footer as well (most easy)
you can use a global state like with Context, Redux, Recoil, etc.

Multiple times Screen render on API call in react native

I have put console log inside of my functional component file. In this file render whenever the API called. If I called API in some other file and the API not related to that file still got logs. I am not sure why. I have checked useEffect and usestate but no clue. Even search from google no clue.
I am using Usestate to store and useEffect to reloaded the the page when data change
I am using apisauce to call the API
React native navigation bottomtab to navigate the screen
Using React.memo but still page rendered.
Any one please help me to resolve this issue. because I am new to react native.
React components automatically re-render whenever there is a change in their state or props. A simple update of the state, from anywhere in the code, causes all the User Interface (UI) elements to be re-rendered automatically.
However, there may be cases where the render() method depends on some other data. After the initial mounting of components, a re-render will occur when:
A component’s setState() method is called.
A component’s forceUpdate() method is called.
Warning: You should normally try to avoid all uses of forceUpdate() and only read from this.props and this.state in render().

How to pass data between same level classes/component without using Navigation in React Native?

Pass data from one class to another class that resides on the same level without navigating in react native.
Passing props or state between two components that are on the same level within your component tree is not possible in React. As a general rule of thumb in React: when two adjacent components need the same data, it is probably better to "lift" the property up a level in your hierarchy and pass the props to both components separately.

How do React Native components avoid recursive state updates?

Consider the TextInput component of React Native. It has a prop called value. I set the value to this.state.text.
It also has a prop onChangeText, which I set to (text) => {this.setState({text: text}).
Setting the state will cause the view to re-render, which will in turn trigger setValue on the component.
This doesn't cause any sort of recursion or undesired behavior with stock components. However, in building my own text input component, I'm finding that using state in value and in onChangeText causes native input events to re-render the view and call setValue.
i.e user types on keyboard, native module emits onChangeText event, JavaScript component receives event and updates state, state change causes re-render, re-render calls setValue on the native component, and the native component swaps out the text with the value received from the initial event (the same value).
The problem is, when you set the text on an iOS or Android native text component, it resets the cursor. So I tried putting in a conditional if(newText != currentText) {setText(newText)}, and this works when you type slow, but when you type fast, things get chaotic and you can't predict the order of events.
I looked at how the React Native authors handle this in their own text component, and they seem to be using some sort of counter (here and here) to synchronize events, but I couldn't keep up with the logic as it was all over the place.
For now, my solution is to not use state in the render function, but then my component depends on render not being called unexpectedly to function properly, and that's just a disaster waiting to happen.
Any insight as how to better design a component like this, or how RN components get around this issue?

What classifies a container in React Native?

I have started working with flex box on React native, on CSS you should set the display to flex but on RN, this is set by default.
What classifies a container object where you can set alignItems, justifyContent?
Does it only need to be a view? or is every single component a potential container?
In my experience objects that honour flex box are ones you would expect to such as View and ScrollView - whereas views like Text do not and are likened to display: inline-block or <span />.
In the example here, The View within the ScrollView honours it's parents flex properties, and similarly the Text inside the View. However the Text objects do not behave in the same way and if I'm not mistaken, would not appear to change when adjusting it's flex properties.
Container components are those React Native components which have access to the store. These components make API calls, do processing and contain the business logic of the app. Container components shouldn't have the view logic or presentational logic. The job of container components is to compute the values and pass them as props to the presentational components.
Difference between Presentational Components Container Components
Purpose How things look (markup, styles) How things work (data fetching, state updates)
Aware of Redux No Yes
To read data Read data from props Subscribe to Redux state
To change data Invoke callbacks from props Dispatch Redux actions
Are written By hand Usually generated by React Native Redux