Is renderItem expected to be called for removed items?
#528
-
|
Hey @MatiPl01 I'm experiencing a case where after removing an item from the data array, the const [items, setItems] = useState([{ id: 1 }, { id: 2 }, { id: 3 }]);
const [itemData, setItemData] = useState({ 1: "A", 2: "B", 3: "C" });
const removeItem = (id: number) => {
// Update external state
setItemData((prev) => {
const next = { ...prev };
delete next[id];
return next;
});
// Remove from grid
setItems((prev) => prev.filter((item) => item.id !== id));
};
const renderItem = ({ item }) => {
const data = itemData[item.id]; // ERROR: undefined after removal
return <Text>{data.name}</Text>;
};
return (
<Sortable.Grid
data={items}
renderItem={renderItem}
keyExtractor={(item) => item.id.toString()}
// ...
/>
);Question: Workaround: Setting The only workaround I found is adding a <Sortable.Grid
key={`grid-${items.length}`}
data={items}
renderItem={renderItem}
// ...
/>This works though hurts the performance. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
|
Hey @JustJoostNL! When do you expect this behavior? Can you share the entire code snippet and tell me how to test it out to reproduce the issue? |
Beta Was this translation helpful? Give feedback.
How is this native object released? If you release it manually while chaning state (items in the context) you can simply move this native state cleanup to the
useEffectused within this component. In general, cleaning up the native state right away in the callback, independent from the component that depends on this state, doesn't seem to be a valid and safe approach.I'd suggest doing something like this if possible:
…