Giter Site home page Giter Site logo

Comments (3)

albertogasparin avatar albertogasparin commented on July 20, 2024

You could but it is a bad pattern and might cause issues with React future improvements.
Basically when you do:

const [data, { setData }] = useData();
if (!data) setData(true);
  1. you might call that function multiple times if the component re-renders again before data is updated
  2. you will trigger an additional re-render, because that component is already subscribed to the store

Even wrapping setData with useMemo might not be enough as Concurrent Mode might run it twice anyway.
Given React owns the re-render schedule and when it happens, you better stick with patterns that ensure data consistency.

Indeed, Containers work in a different way, and the onInit/onUpdate functions ensure that the effects are called only when needed as they rely on the component lifecycle.
However, if you are using the global scope, wrapping your tree in Containers is not mandatory. They are unlikely to cause performance issues, but you can add them as sibling of your main tree, before the consumers, and they will work fine anyway :)

const Root = () => (
  <>
    <DataContainer data={true} isGlobal />
    <MyApp />
  <>
);

^ The above works when either isGlobal or scope are set, but remember that to access scoped stores you need a wrapping container anyway

from react-sweet-state.

adriano-tirloni avatar adriano-tirloni commented on July 20, 2024

Thanks for the reply Alberto, is was able to achieve my need without getting into questionable pattern using a null selector with actions in a previous point in the tree, where I already had access to the state I was going to need downwards.

The isGlobal Container was not needed.

BTW, I'm using NextJs and for some reason react-sweet-state is not playing nicely with its Hot Reload Module, I have to restart the server almost every time a change is made in sweet-state files. Not sure if there is a conflict in my project, but if this does happen might be nice to point out in docs so we are not chasing "intermittent bugs".

from react-sweet-state.

albertogasparin avatar albertogasparin commented on July 20, 2024

Cool.
Regarding NextJs would you mind opening a separate issue? I don't use it so it might take some time before have time to investigate, but can help letting other people know if they experience inconsistencies. Thanks

from react-sweet-state.

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.