React · Middle · Question 54 of 75
Why does this effect re-run on every render even though options looks the same?
const options = { roomId, serverUrl };
useEffect(() => connect(options), [options]);- ✓Correct answer: A new object is created on each render, so the dependency is always different
- 2Effects can't depend on objects
- 3
serverUrlis missing from the dependency list
Explanation
Create the object inside the effect and depend on the primitives ([roomId, serverUrl]), or memoize it. Objects and functions are compared by reference.