GithubHelp home page GithubHelp logo

Comments (4)

dai-shi avatar dai-shi commented on May 29, 2024

Good point.
In general, I'd recommend using useSelector in reactive-react-redux, when migrating from react-redux. And, then convert some of them into useTrackedState and/or useTrackedSelector.

Having multiple useTrackedSelector (and thus useTrackedState) in a component is technically valid as long as you don't hit that caveat's case.
That is:

const foo = useTrackedSelector(state => state.foo);
const foo2 = useTrackedSelector(state => state.foo);
if (foo !== foo2) {
  return 'should not happen in react-redux, but could happen in reactive-react-redux';
}

It's not likely to write such code. I might be missing some other common cases that cause this inconsistent behavior though.

Do you have any suggestion to improve the description in README?

from reactive-react-redux.

dai-shi avatar dai-shi commented on May 29, 2024

Here's another recipe.

// selector definitions
const selectCount = (state) => state.count;
const selectText = (state) => state.text;

// in react-redux
const count = useSelector(selectCount);
const text = useSelector(selectText);

// in reactive-react-redux
const state = useTrackedState();
const count = selectCount(state);
const text = selectText(state);

// in reactive-react-redux with a hack
// --- define a custom hook that returns a hook-like function
const useUseTrackedSelector = () => {
  const state = useTrackedState();
  return (selector) => selector(state);
};
// --- using this custom hook
const useSelector = useUseTrackedSelector();
const count = useSelector(selectCount);
const text = useSelector(selectText);

from reactive-react-redux.

JackCA avatar JackCA commented on May 29, 2024

I think that is a lot better! 👍

Thanks for the thoughtful response. I don't think it's likely to hit that situation but this recipe should help mitigate that.

from reactive-react-redux.

dai-shi avatar dai-shi commented on May 29, 2024

Cool. I will add a link in README to this issue for the time being.

from reactive-react-redux.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.