Quantcast
Viewing latest article 23
Browse Latest Browse All 43

Answer by HMR for Is there a performance penalty when using closures to implement a memoized selector with Reselect?

selectOrdersByCustomer(customerId) Creates a new function every time it is called but still does not need to be a problem if you have a pure component that only re renders when customerId changes:

const PureComponent = React.memo(function Component({ customerId }) {  const orders = useSelector(selectOrdersByCustomer(customerId))})

Another way to memoize it is with useMemo:

function Component({ customerId }) {//not pure/memoized component  const selectMyOrders = useMemo(//memoize the selector based on customerId    () => selectOrdersByCustomer(customerId),    [customerId]  )  const orders = useSelector(selectMyOrders)}

Usually your application is wrapped in <React.StrictMode> and in development mode the useMemo may not behave like you expect it to and creates the selector even though the customerId did not change. This is because StrictMode will try to detect memory leaks and calls hooks multiple times in development.


Viewing latest article 23
Browse Latest Browse All 43

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>