Currently i have started learning in React to call API but when component is rendered on browser side from below code toast message is shown two times instead of single time. Can someone please help me to fix this issue? Thanks in advance!
const [toastShown, setToastShown] = useState(false);
useEffect(() => {
document.title = "All Courses";
axios.get(`${base_url}/courses`).then(
(response) => {
setCourses(response.data);
if (!toastShown) {
toast.success("Courses loaded successfully!");
setToastShown(true);
}
},
(error) => {
toast.error("Something went wrong!");
}
);
}, [toastShown]);
3
Answers
In the development mode,
StrictMode
is enabled which runsuseEffect
twice. Try to build the code and test it inProduction
mode. If it runs twice, try to useAbortController
to stop the first API call, which stops the execution ofthen
.It’s showing twice because you’re performing the operation twice. Note the
useEffect
dependency:So this effect will run if
toastShown
changes. What does this effect do?:toastShown
totrue
So the effect will run once when the component loads with the initial (
false
) value oftoastShown
, and then run again when the component re-renders with the new (true
) value oftoastShown
.You appear to have simply over-engineered yourself into this scenario. Remove
toastShown
entirely:When you update the toastShown state inside the useEffect, it triggers the effect again because toastShown is in the dependency array.You can remove toastShown from the dependency array if you only want the effect to run once when the component mounts.