I have this hook which on mounting of the component requests data and on unmounting clears store for given entity:
export const useFetch = () => {
const dispatch = useDispatch()
const data = useSelector(entitySelector.data) // Selector which returns data from store
const status = useSelector(entitySelector.status) // Selector which returns request status; idle, pending, fulfilled, rejected
useEffect(() => {
if (status === 'idle') {
dispatch(entityActions.request(id))
}
return () => {
dispatch(entityActions.reset())
}
}, [])
return data
}
which in the component is used like:
const data = useFetch()
On the initial load of the component, everything works as expected, data is requested and then loaded and returned object contains data within the store. When component is unmounted reset action is called and store is cleared.
If I switch to other route and back everything works as expected. However, if I open link with the same view, or just save file and let CRA module reloader refresh the component, the useEffect part for fetching data is not called and no data is rendered.
Routes are nested within other component like this:
<div>
<ul>
<li>
<Link to="/foo">Foo</Link>
</li>
<li>
<Link to="/bar">Bar</Link>
</li>
</ul>
<Route path="/foo" component={Foo} />
<Route path="/bar" component={Bar} />
</div>
How do I fix this so that useEffect fetching piece is called upon loading same route again?