I have a React stateless component using redux and hooks. I need to display the number of items on page load (useEffect) and update it every time I add or remove an item (store.subscribe)
useEffect(() => {
setState({
items: store.getState().items.length
});
}, []);
store.subscribe(() => {
setState({
items: store.getState().items.length
});
});
but this is causing the console to display the warning Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function. .
How can I unsubscribe from inside useEffect?