Skip to content

Instantly share code, notes, and snippets.

@cevr
Created June 8, 2019 22:10
Show Gist options
  • Save cevr/f76a7558eb82aee6574df675b34709bb to your computer and use it in GitHub Desktop.
Save cevr/f76a7558eb82aee6574df675b34709bb to your computer and use it in GitHub Desktop.
export default function App() {
const fetchTodos = useActions(actions => actions.fetchTodos);
useEffect(() => {
fetchTodos();
}, [fetchTodos]);
return (
<div>
<h1 style={{ margin: 0 }}>Todo</h1>
<Todos />
<AddTodo />
</div>
);
}
@sysoce
Copy link

sysoce commented Mar 9, 2020

Why is fetchTodos passed as the second argument to useEffect. Won't React always skip the effect cause fetchTodos remains constant?
Should you not pass the state of todos instead?
Thank you and sorry.

@cevr
Copy link
Author

cevr commented Mar 9, 2020

This was more to appease the react-hooks eslint rule 😅

Since fetchTodos is guaranteed to remain constant, it will only run once throughout the lifecycle of this component.

On the first render, the effect has nothing to compare itself to so it will run the fetchTodos.
On every subsequent render, it will not fire due to fetchTodos being identical.

So in practice, this is the same as doing:

useEffect(() => {
    fetchTodos();
  }, []);

@sysoce
Copy link

sysoce commented Mar 11, 2020

Ok, thank you very much for clearing my mind. I see now that it was intentional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment