简体   繁体   English

react-native flatlist 不会在元素更新时重新渲染?

[英]react-native flatlist does not re-render on element update?

So I read that react native FlatList is a PureComponent , so it does not re-render automatically.所以我读到 react native FlatList 是一个PureComponent ,所以它不会自动重新渲染。 But, my current FlatList re-renders when an element is removed or added (so when the array length changes), but if I try to change an element in the data array without changing the length, the FlatList does not re-render.但是,当删除或添加元素时,我当前的 FlatList 会重新呈现(因此当数组长度发生变化时),但是如果我尝试在不更改长度的情况下更改数据数组中的元素,则 FlatList 不会重新呈现。 Is there any way to do this?有没有办法做到这一点?

I have tried extraDatahere , with a flag in my redux state that changes when the action is dispatched, but it does not work.我在这里尝试了 extraData ,在我的 redux 状态中有一个标志,该标志在调度操作时发生变化,但它不起作用。

Fixed it.修复。 My FlatList rendered Child1Components, which had a Child2Component (nested in another level), and that component, for some reason, was not updating - only the Child1Component was updated.我的 FlatList 呈现了 Child1Components,它有一个 Child2Component(嵌套在另一个级别),并且该组件由于某种原因没有更新 - 只有 Child1Component 被更新。 So I every time I wanted to update something in the nested Child2Component, I ran所以每次我想更新嵌套的 Child2Component 中的东西时,我都跑了

this.setState(prevState => ({ flag: !prevState.flag });

in Child2Component to re-render it.在 Child2Component 中重新渲染它。

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

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