V
V
vladislav31012021-03-24 14:14:04
React
vladislav3101, 2021-03-24 14:14:04

How to do redux dispatch inside useEffect?

const function Component = () => {
   const data = useSelector(state => state.data);
   
useEffect(async () => {
    const {token} = queryString.parse(location.search):
   const response = await fetch(...).then(res => res.json());
   if (!response.status) {
        dispatch(updateError(true));
   }
}, []);
};

I get the expected result, but in console.log the component is updated infinitely or a large number of times

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
black1277, 2021-03-24
@black1277

misusing async with the useEffect hook. You need to define an asynchronous function in the body of the hook and then call it. Read more here https://www.robinwieruch.de/react-hooks-fetch-data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question