@Ayman-Shakil192
Posted
Hey Leandro! 👋
Amazing job completing the challenge! Your solution looks amazing!
Now to answer your query , The reason why your code is fetching the advice object twice is because of the initial value of the get state variable which is set to 1.
When the component mounts, the useEffect hook is triggered, sending a GET request to the specified URL and updating the data state variable with the response data. This also triggers a re-render of the component.
When the component re-renders, the get state variable is incremented by 1 with setGet(c => c + 1). This causes the useEffect hook to run again, triggering another GET request to the API and another update to the data state variable, followed by another re-render.
To fix this, you can set the initial value of the get state variable to 0. This will cause the useEffect hook to only run once on the initial mount of the component, and not on subsequent re-renders. Here's the updated code snippet
const [get, setGet] = React.useState(0); // set initial value to 0
Marked as helpful