Search code examples
reactjsaxiosuse-effectuse-state

useSate doesn't execute in axios promise


this is a part of a react component

  const listId = props.listId;
  const [list, setList] = useState({});
  useEffect(() => {
    let isMounted = true;
    axios.get(`http://localhost:9000/lists/${listId}`).then((res) => {
      if (isMounted) {
        setList({ ...res.data });
      }
    });
    return () => {
      isMounted = false;
    };
  });

for some reason the setList doesn't execute


Solution

  • Check out this codesandbox, it works fine.

    codesandbox Link: https://codesandbox.io/s/interesting-bush-5v3hf?file=/src/App.js