question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React Redux example doesn't seem to make sense in a real-world context

See original GitHub issue

Hi there, I’m trying to understand this example:

import { useSelector } from 'react-redux';

const getScore = memoize(state => ({
  score: heavyComputation(state.a + state.b),
  createdAt: Date.now(),
}));

const Component = ({ id }) => {
  const { score, title } = useSelector(useCallback(memoize(state => ({
    score: getScore(state),
    title: state.titles[id],
  })), [id]));
  return <div>{score.score} {score.createdAt} {title}</div>;
};

I realize the example is more about how this library can interoperate with React Redux than about giving a practical scenario, but it doesn’t really help me to understand how you would use proxy-memoize to create create a memoized calculation based on both the Redux store’s state and the component’s props. The way it’s written above, useCallback will simply tear down and rebuild the memoized function whenever id changes, so there will be never any opportunity to hit the cache in this scenario.

A simpler and more efficient way of formulating the above is this:

const Component = ({ id }) => {
  const score = useSelector(getScore);
  const title = useSelector(state => state.titles[id]);
  return <div>{score.score} {score.createdAt} {title}</div>;
};

If you were trying to actually calculate expensive derived data based on both state and props with this library, how are you supposed you do it?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:19 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, Aug 24, 2021

I assume this issue is not very active now. While the issue is not solved, let’s continue with new issues. Just created two:

Please feel free to open new issues for questions and suggestions.

1reaction
lostfictionscommented, Feb 22, 2021

Yes, I think I would be up for this! I may spend a bit of time trying proxy-memoize in my project first to make sure I better understand how to use it idiomatically. It’s been a while since I’ve used reselect myself.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding Redux: A tutorial with examples
In this tutorial, we'll show you what Redux is, why you should use it, and how it works. We'll demo using a simple...
Read more >
Redux vs. The React Context API - Dave Ceddia
In this post I want to cover how the new Context API works, how it is similar to Redux, when you might want...
Read more >
Understanding Redux: The World's Easiest Guide to ...
This is a comprehensive (but simplified) guide for absolute Redux beginners, or any who wants to re-evaluate their understanding of the ...
Read more >
React Redux Tutorial for Beginners: The Complete Guide (2020)
This holds particularly true in React. Yeah, you can get by with keeping the state within a parent React component (or in context)...
Read more >
Why I Stopped Using Redux - DEV Community ‍ ‍
Redux was a revolutionary technology in the React ecosystem. It enabled us to have a global store with immutable data and fixed the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found