? 1. Can I set state inside a useEffect hook?
In principle, you can set state freely where you need it - including inside useEffect
and even during rendering. Just make sure to avoid infinite loops by settting Hook deps
properly and/or state conditionally.
? 2. Lets say I have some state that is dependent on some other state. Is it appropriate to create a hook that observes A and sets B inside the useEffect hook?
You just described the classic use case for useReducer
:
useReducer
is usually preferable touseState
when you have complex state logic that involves multiple sub-values or when the next state depends on the previous one. (React docs)When setting a state variable depends on the current value of another state variable, you might want to try replacing them both with
useReducer
. [...] When you find yourself writingsetSomething(something => ...)
, it’s a good time to consider using a reducer instead. (Dan Abramov, Overreacted blog)
let MyComponent = () => {_x000D_
let [state, dispatch] = useReducer(reducer, { a: 1, b: 2 });_x000D_
_x000D_
useEffect(() => {_x000D_
console.log("Some effect with B");_x000D_
}, [state.b]);_x000D_
_x000D_
return (_x000D_
<div>_x000D_
<p>A: {state.a}, B: {state.b}</p>_x000D_
<button onClick={() => dispatch({ type: "SET_A", payload: 5 })}>_x000D_
Set A to 5 and Check B_x000D_
</button>_x000D_
<button onClick={() => dispatch({ type: "INCREMENT_B" })}>_x000D_
Increment B_x000D_
</button>_x000D_
</div>_x000D_
);_x000D_
};_x000D_
_x000D_
// B depends on A. If B >= A, then reset B to 1._x000D_
function reducer(state, { type, payload }) {_x000D_
const someCondition = state.b >= state.a;_x000D_
_x000D_
if (type === "SET_A")_x000D_
return someCondition ? { a: payload, b: 1 } : { ...state, a: payload };_x000D_
else if (type === "INCREMENT_B") return { ...state, b: state.b + 1 };_x000D_
return state;_x000D_
}_x000D_
_x000D_
ReactDOM.render(<MyComponent />, document.getElementById("root"));
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.0/umd/react.production.min.js" integrity="sha256-32Gmw5rBDXyMjg/73FgpukoTZdMrxuYW7tj8adbN8z4=" crossorigin="anonymous"></script>_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.0/umd/react-dom.production.min.js" integrity="sha256-bjQ42ac3EN0GqK40pC9gGi/YixvKyZ24qMP/9HiGW7w=" crossorigin="anonymous"></script>_x000D_
<div id="root"></div>_x000D_
<script>var { useReducer, useEffect } = React</script>
_x000D_
? 3. Will the effects cascade such that, when I click the button, the first effect will fire, causing b to change, causing the second effect to fire, before the next render?
useEffect
always runs after the render is committed and DOM changes are applied. The first effect fires, changes b
and causes a re-render. After this render has completed, second effect will run due to b
changes.
let MyComponent = props => {_x000D_
console.log("render");_x000D_
let [a, setA] = useState(1);_x000D_
let [b, setB] = useState(2);_x000D_
_x000D_
let isFirstRender = useRef(true);_x000D_
_x000D_
useEffect(() => {_x000D_
console.log("useEffect a, value:", a);_x000D_
if (isFirstRender.current) isFirstRender.current = false;_x000D_
else setB(3);_x000D_
return () => {_x000D_
console.log("unmount useEffect a, value:", a);_x000D_
};_x000D_
}, [a]);_x000D_
useEffect(() => {_x000D_
console.log("useEffect b, value:", b);_x000D_
return () => {_x000D_
console.log("unmount useEffect b, value:", b);_x000D_
};_x000D_
}, [b]);_x000D_
_x000D_
return (_x000D_
<div>_x000D_
<p>a: {a}, b: {b}</p>_x000D_
<button_x000D_
onClick={() => {_x000D_
console.log("Clicked!");_x000D_
setA(5);_x000D_
}}_x000D_
>_x000D_
click me_x000D_
</button>_x000D_
</div>_x000D_
);_x000D_
};_x000D_
_x000D_
ReactDOM.render(<MyComponent />, document.getElementById("root"));
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.0/umd/react.production.min.js" integrity="sha256-32Gmw5rBDXyMjg/73FgpukoTZdMrxuYW7tj8adbN8z4=" crossorigin="anonymous"></script>_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.0/umd/react-dom.production.min.js" integrity="sha256-bjQ42ac3EN0GqK40pC9gGi/YixvKyZ24qMP/9HiGW7w=" crossorigin="anonymous"></script>_x000D_
<div id="root"></div>_x000D_
<script>var { useReducer, useEffect, useState, useRef } = React</script>
_x000D_
? 4. Are there any performance downsides to structuring code like this?
Yes. By wrapping the state change of b
in a separate useEffect
for a
, the browser has an additional layout/paint phase - these effects are potentially visible for the user. If there is no way you want give useReducer
a try, you could change b
state together with a
directly:
let MyComponent = () => {_x000D_
console.log("render");_x000D_
let [a, setA] = useState(1);_x000D_
let [b, setB] = useState(2);_x000D_
_x000D_
useEffect(() => {_x000D_
console.log("useEffect b, value:", b);_x000D_
return () => {_x000D_
console.log("unmount useEffect b, value:", b);_x000D_
};_x000D_
}, [b]);_x000D_
_x000D_
const handleClick = () => {_x000D_
console.log("Clicked!");_x000D_
setA(5);_x000D_
b >= 5 ? setB(1) : setB(b + 1);_x000D_
};_x000D_
_x000D_
return (_x000D_
<div>_x000D_
<p>_x000D_
a: {a}, b: {b}_x000D_
</p>_x000D_
<button onClick={handleClick}>click me</button>_x000D_
</div>_x000D_
);_x000D_
};_x000D_
_x000D_
ReactDOM.render(<MyComponent />, document.getElementById("root"));
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.0/umd/react.production.min.js" integrity="sha256-32Gmw5rBDXyMjg/73FgpukoTZdMrxuYW7tj8adbN8z4=" crossorigin="anonymous"></script>_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.0/umd/react-dom.production.min.js" integrity="sha256-bjQ42ac3EN0GqK40pC9gGi/YixvKyZ24qMP/9HiGW7w=" crossorigin="anonymous"></script>_x000D_
<div id="root"></div>_x000D_
<script>var { useReducer, useEffect, useState, useRef } = React</script>
_x000D_