简体   繁体   English

React Native - FlatList - 内部状态

[英]React Native - FlatList - Internal State

I'm using a FlatList in order to implement a Store View in my react-native app. 我正在使用FlatList,以便在我的react-native应用程序中实现Store View。

I'm retrieving the store items from an API Call that is done in the componentDidMount cycle step of my component and then store it in my local state . 我正在从我在组件的componentDidMount循环步骤中完成的API调用中检索商店项目,然后将其存储在我的本地state

I've found in the Flatlist documentation this sentence : 我在Flatlist文档中找到了这句话:

Internal state is not preserved when content scrolls out of the render window. 当内容滚出渲染窗口时,不会保留内部状态。 Make sure all your data is captured in the item data or external stores like Flux, Redux, or Relay. 确保在项目数据或外部存储(如Flux,Redux或Relay)中捕获所有数据。

I was wondering what it meant. 我想知道这意味着什么。 I'm just using my local state and it seems to work just fine. 我只是使用我的本地状态,它似乎工作得很好。 Is there any issue by doing so ? 这样做有什么问题吗? Could you provide me more informations about this particular point ? 你能否就这一点向我提供更多信息?

Also, if you have any advice or optimization about my use-case, feel free to add them. 此外,如果您对我的用例有任何建议或优化,请随意添加它们。

Thanks for your time. 谢谢你的时间。

Let's say you have a Contacts component with a FlatList rendering Contact component for each item in your data. 假设您有一个Contacts组件,其中包含数据中每个项目的FlatList呈现Contact组件。 Let's also say that these contacts are selectable. 我们也说这些联系人是可选择的。 If you store these select value ( for example selected: true ) in Contact components internal state and not in Contacts components state, when the item scrolls out its going to be unmounted and the state of that item going to be reset. 如果在Contact组件内部状态而不是Contacts组件状态中存储这些选择值(例如selected: true ),则当项目滚动时将卸载它,并且该项目的状态将被重置。 If you hold it in the global data it will be created with the last state. 如果将其保存在全局数据中,则将使用最后一个状态创建它。

Hope I was able to explain. 希望我能够解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM