Passing an empty array as the second argument to useEffect makes it only run on mount and unmount, thus stopping any infinite loops.
useEffect(() => {
setIngredients({});
}, []);
This was clarified to me in the blog post on React hooks at https://www.robinwieruch.de/react-hooks/
More Related Contents:
- How to fix missing dependency warning when using useEffect React Hook
- How to compare oldValues and newValues on React Hooks useEffect?
- Does React batch state update functions when using hooks?
- React-hooks. Can’t perform a React state update on an unmounted component
- With useEffect, how can I skip applying an effect upon the initial render?
- React useState cause double rendering
- React Hook : Send data from child to parent component
- Line 0: Parsing error: Cannot read property ‘map’ of undefined
- In general is it better to use one or many useEffect hooks in a single component? [closed]
- React hooks useEffect only on update?
- Cannot read property ‘history’ of undefined (useHistory hook of React Router 5)
- Set types on useState React Hook with TypeScript
- Stop useEffect from running on mount
- useNavigate() may be used only in the context of a component
- Testing React components that fetches data using Hooks
- React Hooks: useEffect() is called twice even if an empty array is used as an argument
- Lazy initial state – What it is and how to use it?
- React Hooks: how to wait for the data to be fetched before rendering
- Does a render happen before function in React Hooks useEffect is called?
- React Hooks – Modified state not reflecting immediately
- React Hooks – Modified state not reflecting immediately
- React Hooks: Why is .current null for useRef Hook?
- is it possible to React.useState(() => {}) in React?
- How to test a react component that is dependent on useContext hook?
- What is the expected return of `useEffect` used for?
- ReactJS – prevState in the new useState React hook?
- Property ‘value’ does not exist on type ‘never’. when use useRef hook in mui
- How to create dynamic refs in functional component- Using useRef Hook
- Put A Warning If Page Refresh in ReactJS
- React – Is useState ‘s setter function able to change?