score:132
The useEffect
and setState
will cause extra renders on every change: the first render will "lag behind" with stale data and then it'll immediately queue up an additional render with the new data.
Suppose we have:
// Maybe I'm running this on a literal potato
function expensiveCalculation(x) { return x + 1; };
Lets suppose x
is initially 0:
- The
useMemo
version immediately renders1
. - The
useEffect
version rendersnull
, then after the component renders the effect runs, changes the state, and queues up a new render with1
.
Then if we change x
to 2:
- The
useMemo
runs and3
is rendered. - The
useEffect
version runs, and renders1
again, then the effect triggers and the component reruns with the correct value of3
.
In terms of how often expensiveCalculation
runs, the two have identical behavior, but the useEffect
version is causing twice as much rendering which is bad for performance for other reasons.
Plus, the useMemo
version is just cleaner and more readable, IMO. It doesn't introduce unnecessary mutable state and has fewer moving parts.
So you're better off just using useMemo
here.
score:7
I would say other than the async nature, there might be some difference in terms how they are designed.
useEffect
is a collective call, async or not, it's collected after all components are rendered.
useMemo
is a local call, which has only something to do with this component. You could just think of useMemo
as another assignment statement with benefits to use the assignment from last update.
This means, useMemo
is more urgent, and then useLayoutEffect
and the last being useEffect
.
score:13
I think there are two main points you should consider when choosing between them.
- Time when function called.
useEffect
called after component has been rendered, so you can access DOM from it. For example, this is important if you want to access DOM elements via refs.
- Semantic guarantees.
useEffect
guarantees that it will not be fired if dependencies have not changed. useMemo
does not give such guarantees.
As stated in the React documentation, you should consider useMemo as pure optimization technique. Your program should continue to work correctly even if you replace useMemo with regular function call.
useEffect
+ useState
can be used to control updates. Even to break-up circular dependencies and prevent infinite update loops.
Source: stackoverflow.com
Related Query
- useMemo vs. useEffect + useState
- React: useEffect vs useMemo vs useState
- variable in useState not updating in useEffect callback
- useMemo vs useState for React hooks constants
- useState in useEffect does not update state
- Use case for useLayoutEffect + useState vs useMemo
- useEffect or useMemo for API functions?
- React useEffect with useState and setInterval
- Use useState value in useEffect without making the state update useEffect
- How to test useState and useEffect react hooks
- useEffect to show snackbar if message is not null, and how to use useState with that?
- React: Confusing behavior with useEffect and/or useState hooks
- useState setter not updating state when called in useEffect
- Difference and use cases with useEffect and useMemo hooks on React
- Using useEffect to move react query state to useState
- Using useState hook in useEffect on history.listen
- Bug with useState / useEffect react spread operator
- useState variable is called before useEffect API call
- Infinity loop using useState with useEffect in React
- Is okay to useMemo instead of useEffect for calling side-effect functions
- In react when state changes, are useState and useEffect also updated?
- Calling useState inside useEffect on DOM load
- Inifnite render error when setting value via useState inside a useEffect only when testing
- reactjs Avoid Infinite loop using the useState and useEffect
- "Synchronous" useState without useEffect
- React useEffect and useState interaction
- Can a useMemo memoized value be updated with useEffect from within the component?
- useState vs. useEffect - setting initial value
- "TypeError: Cannot read property 'name' of undefined" when using useState and useEffect
- React Hooks: using useState inside useEffect doesn't set the state immediately after first rendering
More Query from same tag
- MobX not setting observable properly in my class
- Connecting my React App to a MySQL Database on a PHP Server
- Looping through an array in ReactJs
- CSS and Bootstrap - full page image not working
- Cannot use "import / require" into service worker file
- How do I correctly configure eslint to lint JSX files in SublimeText3
- How to create Checkboxes with CheckAll checkbox in react hooks?
- The React variable changes, but it is not rendered... why?
- How to test react-router with enzyme
- Not able to set focus on the Dropdown component in Primereact
- OpenSeadragon missing prop in types definition
- Javascript Reference function from fat arrow function, or how to trigger function every X sec
- React state array (first input not working)
- Port doesn't recognized while running multiple app of react on windows
- How to truncate a overflowed text in a react element with middle ellipsis?
- Text input added to Material UI Autocomplete Paper Component losing focus after each character typed as options filter
- How can I set the state of a component on video end in react?
- how to use map and boolean variables inside paths? (can't return the attributes using routers)
- React setState in function or setState in useCallback
- Function runs forever in React Component
- load local mp4 file as source for video tag in React Gatsby website
- How can I change the fontweight of an item at click method - React
- Can I call separate function in useEffect?
- react router version 4 not showing anything
- Try-Catch functionality behind the scenes
- How to: React serialise multidimensional array and unserialise it in PHP
- How can I capture mouse click and drag events for a MapControl in react-leaflet
- useState hook setter is running only once when called by an EventListener
- For different route, same react component does not get mounted
- React Router Dom v.5 --> v.6 - matchPath bug