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]);

Explanation

Create the object inside the effect and depend on the primitives ([roomId, serverUrl]), or memoize it. Objects and functions are compared by reference.

Further reading

Problem with a question

React · Middle · question 54

Test yourself

Take the React · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz