Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding dynamic data on the fly to the data array will not update the internal FlatList data #23

Open
Symyon opened this issue Jun 15, 2021 · 1 comment

Comments

@Symyon
Copy link

Symyon commented Jun 15, 2021

Data array sent to FlatList is first saved into state on constructor and then used in FlatList internal component. So, when you modify an element of the data array while list is rendered on the screen, that new data will not be sent to the specific element as 'item'.

I have FlatListSlider that takes a custom render component for each item. Each item has a feature to upload an image and then display it in that component when upload succeeded. So, basically, the custom component has an Image component inside that will get its 'uri' updated on the fly. When I do that, the image will not appear like nothing happened, the data array sent to FlatListSlider is updated. It's enough to navigate to another screen and come back to get the image to show.

I checked internal code and it seems the data sent to FlatListSlider is cached as state on constructor call. So change of the data array props will not update the internal FlatList data array. Changing "data={this.state.data}" to "data={this.props.data}" will fix the issue.

Was that done for performance considerations? Why not use this.props.data directly and why caching it to state?

@LuamLeite
Copy link

Thank you very much for this. I have been stuck for weeks thinking the problem was my state management, I can't tell you how much I read the documentation and tried different approaches to make this work. And all along it was just the component by itself that had an error.
Lesson learned, next time I will check the code of the component as well. Thank you again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants