Repository navigation
Will useMemo replace reselect? #386
Description
Activity
With only a cursory glance, I would assume that
useMemocannot replacereselectsimply becauseuseMemois a hook and therefore will only work with functional components. Whilereselectworks with any type of component.As far as I know the original intent was to couple
reselectwithreact-redux. Seeing as they plan on adding support for some kind ofuseReduxlike functionality inreact-reduxI'd figure that could be coupled withuseMemoeventually. I am pretty sure to get a real answer to your question we will have to wait to see what it is exactlyreact-reduxcreates in regards to a hook, and whether or not that works well withuseMemo.With that said,
reselectcan be used anywhere, as it has no dependencies. So if it was necessary to use with a theoretical new hook instead ofconnect(), it may be able to be repurposed for that use case.Reacted by Jacky Hu and StefanaReselect is indeed useful anywhere, and can be used totally independent of Redux. Really, its competition is libs like
memoize-one, notuseMemo().For Redux usage, we recommend using selectors anywhere you access the Redux state tree, not just in
mapStatefunctions. That includes thunks, sagas, middleware, and so on. See my post Idiomatic Redux: Using Reselect Selectors for Encapsulation and Performance for more details.Reacted by Cory House, Yann RENAUDIN, Benjamin Delmas, King David Martins, Andrew Craswell, Justin Linn, Shagen A., Firzinat Khuzeev, Amir Mohammad Moradi, Nikita Gordeev and 5 moreReacted by Vincent Taverna, King David Martins, Justin Linn, Benjamin Delmas, Gerardo Cordero and EnzoHa, I dunno what I was thinking. I know Hooks only work in React components. Sigh. Thanks for your patience and the excellent response guys! 👍
Closing. :)
But in terms of using it within react apps, will there be any reasons for not replacing it with memo hooks?
Reacted by Yarin Dekel, Frederick Chapman, Dan Seethaler, Jake Boone, Erik Cupal, Kevin Duret, Steven Cummings, Udi Elenberg, Nathan Vander Wilt, Bill Cryer and 24 moreOne stylistic difference is: with
reselectand e.g.react-reduxyour component rerenders only when a selected/memoized value changes. WithuseMemo, the component renders anyways. Yes, you could create a wrapper / HOC that renders your component and use useMemo in there. That's essentially what react-redux'sconnectdoes. So well. To me at least, it seems to come down to a matter of individual tasteBut rendering is not the same as commit in react terms. For detailed explanation on the same
https://kentcdodds.com/blog/fix-the-slow-render-before-you-fix-the-re-renderNo, they are different.
useMemomemorizes data per instance butreselectis global.Reacted by Anson Kao, Harris Thompson, Glow, Fábio D. Batista, Kirill Taletski, MD, Yevhen Kozlov, Anton Shokut, Zach Pang, DecafDev and 2 moreNo, they are different.
useMemomemorizes data per instance butreselectis global.I'm curious whether the memory usage of
useMemo()will be greater than that of selector if there are a lot of instances.
Is there anything reselect offers that useMemo can't handle?