I have to fetch an API when a state changes, i've used useEffect adding the dependency like this
const [ activeLottery, setActiveLottery ] = useState();
useEffect(() => {
console.log('fetching')
}, [activeLottery])
return (
<div className="container">
<Carousel
lotteries={ lotteries }
isLoading={ isLoading }
slideClick={ setActiveLottery }
/>
</div>
);
The Component Carousel has an onClick function which changes activeLottery and it works properly since the state is mutated. But it executes the fetch when the component is rendered. Shouldn't do it when activeLottery changes?
Any hint would be much appreciated.
thanks