0

I have some struggles with loading data with NextJs, useEffect function is loading twice.

So, I have one component, and inside a simple call method to get my list like so:

const ListCategories = () => {
    const dispatch = useDispatch();
    const forumState = useSelector((state: RootState) => state.forumData);

    useEffect(() => {
        async function fetchData() {
                try {
                    const response = await categories();
                    if (!isGenericApiError(response.data)) {
                        dispatch(setForumData(response.data));
                    }
                } catch (err) {
                    dispatch(setForumData(undefined)); //signal error?
                }
        }

        fetchData();
    }, [dispatch]);

    return (here is some elements)

Why is useEffect loaded twice in this case? I don't want to set reactStrictMode to false, and it is a solution, but is there a better one?

Youssouf Oumar
  • 29,373
  • 11
  • 46
  • 65
HOHOOH
  • 95
  • 7

0 Answers0