Replies: 1 comment
-
This is a "problem" you'll see in a lot of Some possible solutions: Stringify your object/array propSee https://stackoverflow.com/questions/59467758/passing-array-to-useeffect-dependency-list Though because you are using a map, which can't be stringified by default, see https://stackoverflow.com/questions/29085197/how-do-you-json-stringify-an-es6-map#:~:text=You%20can't.,only%20allows%20strings%20as%20keys. Trigger the effect when setting the value insteadfunction A() {
const map = useMap();
const doSomething = () => {
map.forEach((value, key) => {
// do something according to the map
// .....
});
}
const setMapAndDoSomething = (key, value) => {
map.set(key, value)
doSomething()
}
return (
<button onClick={() => setMapAndDoSomething("foo", "bar")}>
something change the map
</button>
);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The useMap hook return a MapLike instance doesn't change between render but its methods will trigger render according to the document
Since the map doesn't change between render, The following code doesn't work.
Though the component renreder when button click, but map has the same reference the useEffect is not run.
How to correctly use the map in dependencies list?
Beta Was this translation helpful? Give feedback.
All reactions