question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected behavior when map() is applied to the property of the tracked state

See original GitHub issue

Greetings! 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:closed
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, Oct 9, 2020

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 tried console.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 of useTrackedState if you want to detect the reference change, and all the values are not changed.

const myArrayInStore = useSelector(useCallback(state => state.myArrayInStore, []));

Hope it helps!

0reactions
dai-shicommented, Dec 2, 2020

Closing this as answered.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found