I want to use useEffect
to fire 2 functions (which are redux actions creators) - one function when component mounts, and the other one on unmount, like so:
// ...
function MyComp(props) {
function handleSelectItem(itemId) {
const data = props.data.filter(/* some filter logic depending on itemId */);
props.selectItem({ itemId, data });
}
useEffect(() => {
handleSelectItem(1);
return props.deselectItem;
}, []);
}
export default connect(null, {selectItem, deselectItem})(MyComp)
It works, but the react-hooks/exhaustive-deps
eslint rule telling me that my useEffect
has 2 dependencies which I should include into the dependencies array (useEffect's second argument) or remove the array completely. Second option is not suitable as I don't need these functions to be called on each render. Hence, I need to include them both. There is an article regarding including functions into dependency array. Things described there is pretty much clear to me, but I still confused about the redux action creators in dependencies array and how react will shallowly compare this functions. For me this is a little bit hard to understand. What is the right way to resolve this particular case what is the right way to use redux action creators in useEffect in general?