React Native - FlatList - 内部状态 [英] React Native - FlatList - Internal State

查看:61
本文介绍了React Native - FlatList - 内部状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 FlatList 以在我的 react-native 应用程序中实现 Store 视图.

I'm using a FlatList in order to implement a Store View in my react-native app.

我正在从组件的 componentDidMount 循环步骤中完成的 API 调用中检索商店项目,然后将其存储在我的本地 state 中.

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.

我在 Flatlist 文档中发现了这句话:

I've found in the Flatlist documentation this sentence :

当内容滚出渲染窗口时不会保留内部状态.确保您的所有数据都在项目数据或外部存储(如 Flux、Redux 或 Relay)中捕获.

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.

我想知道这是什么意思.我只是使用我的本地状态,它似乎工作得很好.这样做有什么问题吗?你能给我提供更多关于这个特定点的信息吗?

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.

感谢您的时间.

推荐答案

假设您有一个 Contacts 组件和一个 FlatList 渲染 Contact 组件,用于数据中的每个项目.假设这些联系人是可选择的.如果您将这些选择值(例如 selected: true)存储在 Contact 组件内部状态而不是 Contacts 组件状态,则当项目滚动时它将被卸载并且该项目的状态将被重置.如果你将它保存在全局数据中,它将使用最后一个状态创建.

Let's say you have a Contacts component with a FlatList rendering Contact component for each item in your data. 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. If you hold it in the global data it will be created with the last state.

希望我能解释一下.

这篇关于React Native - FlatList - 内部状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆