But isn't React.memo making us a disservice, obfuscating our questionable design decisions? Like in this case we can extract all counting logic into a separate Counter component and actually fix the problem instead of sweeping it under React.memo rug.
Thank you, the post is very useful. If we use memo in every child component, will there be any other problem? What do you think about the fact that some developers and articles recommend that use fewer hooks like 'useCallback'、'memo'?
There shouldn't be any problem per se if every child component is memoized but something like that is generally not recommended. Unless of course you run into a scenario where it makes sense to do it i.e. you have child components doing some heavy lifting but giving the same result.
useCallback and useMemo (which are equivalent with a slight difference in syntax by the way) do the same thing in that they memoize the result of a function call but, being hooks, they're restricted to be used from within a functional component. React.memo() is much more flexible. In fact, it is extremely common to have a separate file for your component and memoize it just before exporting it. Something like this:
After being a freelance web developer for 12 years, I got a fulltime gig at Leadtech, Barcelona as a front-end developer. I'm the advocate of switching to Gutenberg blocks on our current WP web sites.
React.memo() is different in that it expects a React component and will compare that component's props to decide whether to re-evaluate the component or not. useMemo() and useCallback() are hooks that watch a dependency array to decide whether to re-evaluate a given function or not.
While specific use-cases for useMemo() and useCallback() can be argued, they're more or less equivalent with a slightly different syntax.
But isn't
React.memomaking us a disservice, obfuscating our questionable design decisions? Like in this case we can extract all counting logic into a separateCountercomponent and actually fix the problem instead of sweeping it underReact.memorug.In this case sure but this is just an example to keep things simple.
Thank you, the post is very useful. If we use memo in every child component, will there be any other problem? What do you think about the fact that some developers and articles recommend that use fewer hooks like 'useCallback'、'memo'?
There shouldn't be any problem per se if every child component is memoized but something like that is generally not recommended. Unless of course you run into a scenario where it makes sense to do it i.e. you have child components doing some heavy lifting but giving the same result.
useCallbackanduseMemo(which are equivalent with a slight difference in syntax by the way) do the same thing in that they memoize the result of a function call but, being hooks, they're restricted to be used from within a functional component.React.memo()is much more flexible. In fact, it is extremely common to have a separate file for your component and memoize it just before exporting it. Something like this:Here's a great article for when to use useMemo and useCallback - kentcdodds.com/blog/usememo-and-us...
great post! small typo on
dicate=>dictatethat can be corrected easilyThanks!
This really helped improve my react native app. Thanks
Glad to hear that!
Thank you, I finally got it! Is
React.memo()the same asReact.useMemo()? If so, how doesReact.useMemo()compare toReact.useCallback()?React.memo()is different in that it expects a React component and will compare that component's props to decide whether to re-evaluate the component or not.useMemo()anduseCallback()are hooks that watch a dependency array to decide whether to re-evaluate a given function or not.While specific use-cases for
useMemo()anduseCallback()can be argued, they're more or less equivalent with a slightly different syntax.Nice one. It was easy to understand.
Thanks for your feedback!
Thank you. Easy to understand!
Thanks!
Thank you for the post. it is very informative and simple to understand.
Thanks!
Isn't the output supposed to show "hola" at least once?