How do I prevent my React component from fetching images every time the component is rendered and to fetch them from the store instead? I don't want to get rate limited by doing the API call over and over again, but the problem with useEffect
is that it seems to be unaware of the variables being set "outside" of the effect. It seems to completely ignore !images.length
and when I log the value for images.length
it is always 0
:(
Images.tsx
const dispatch = useDispatch();
const images = useSelector(selectedImages);
useEffect(() => {
if (!images.length) {
dispatch(fetchImages());
}
}, []);