Unexpected behavior when map() is applied to the property of the tracked state
See original GitHub issueGreetings! I have an action in my reducer which map over an array in the store:
setMyArrayInStore: ({ payload, store }) => {
const { rowData, type } = payload;
return {
myArrayInStore: store.myArrayInStore.map(
(item) => {
if (item.field !== rowData.field) {
return item;
}
return {
...item,
[type]: rowData[type],
};
}
),
};
},
In another part of my code, I’m using myArrayInStore as a useEffect() dependency.
const { myArrayInStore } = useTrackedState();
useEffect(() => {
...some actions on myArrayInStore change
}, [myArrayInStore],
Without react-tracked useEffect() recognize that myArrayInStore was recreated and fires actions inside.
When I change the code to use react-tracked, useEffect() no longer fire actions.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
Why unfavorable React keys lead to unpredictable behavior
The consequence is that elements are unnecessarily re-rendered or, in the worst case, component states are mixed up, resulting in serious bugs.
Read more >GitHub - openlayers/ol-mapbox-style
To apply the properties of the Mapbox Style's background layer to the map or a VectorTile layer, use the applyBackground() function.
Read more >Breaking changes in EF Core 5.0 - Microsoft Learn
GetColumnName() still returns the name of a column that a property is mapped to, but this behavior is now ambiguous since EF Core...
Read more >Maps JavaScript API | Google Developers
MapOptions object used to define the properties that can be set on a Map. Properties. backgroundColor optional. Type: string optional.
Read more >User properties and event properties - Amplitude Help Center
Amplitude's SDKs track the following user properties by default: ... Updates to user property values are not applied retroactively and the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the example! I understand what’s happening.
This happens because you change the state object reference, but the internal values are not changed. When
useTrackedState
compares two objects (prev state and next state), it doesn’t find any differences (= what you display is the same), so it will not re-render (not commit). You might have triedconsole.log(myArrayInStore)
and see something, but this only mean it invokes the function (which must be pure), but it’s actually not committed (I know this behavior is not desired, but can’t be helped for now. v2 with useMutableSource will probably fix this.)Here’s the example that changes what it shows. https://codesandbox.io/s/not-working-store-forked-inedh?file=/src/MyComponent.js
Another workaround is to use
useSelector
instead ofuseTrackedState
if you want to detect the reference change, and all the values are not changed.Hope it helps!
Closing this as answered.