I notice that if I have a useEffect
which sets a variable which is not used in any of the rendered output, the entire page still gets re-rendered after that useEffect
.
Suppose I have
const [users, setUsers] = useState<DropDownItem[]>([]);
// No other effects. This effect sets an unused variable.
useEffect(() => {
fetchUsers(); // This function will call setUsers to set the users
}, []);
// Rendered part
return (
<>
..
{/* Does NOT use "users" in any way */}
</>
);
The debugger shows
- Initial render
- useEffect
- Re-render which I can’t explain. There are no other effects, and the one effect’s var isn’t used in rendering. So why is this happening?