matthewp / haunted

React's Hooks API implemented for web components 👻
BSD 2-Clause "Simplified" License
2.58k stars 92 forks source link

Infinite loop if effect schedules an update and then throws #468

Closed cristinecula closed 1 year ago

cristinecula commented 1 year ago

This code will cause an infinite loop. The effect hook will fail to update the lastValues, because of the error stopping the execution of call. This means that on the next render, the effect will run again, even though the deps array is still [], thus scheduling another update, causing the infinite loop.

  function App() {
      const [state, setState] = useState(0);

      useEffect(() => {
        // an update is scheduled
        setState((state) => state! + 1);
        // an error is thrown
        throw new Error("Unexpected error");
      }, []);

      return state;
    }

I will submit a potential fix momentarily.