qosenergy / shared-store-hook Goto Github PK
View Code? Open in Web Editor NEWEasily share state data between several React components. It's just like useState, but shared.
Easily share state data between several React components. It's just like useState, but shared.
Is there a way to make this awesome statemanagement library compatible with SSR?
Now React throws this warning:
Warning: useLayoutEffect does nothing on the server, because its effect cannot be encoded into the server renderer's output format. This will lead to a mismatch between the initial, non-hydrated UI and the intended UI. To avoid this, useLayoutEffect should only be used in components that render exclusively on the client
BTW thx for making a alternative for "use-global-hook" this library is way beter and migrating was quite easy thanks to you.
When a new subscribing component calls a shared store hook, the state it will get back is first computed, and then adding this component to the subscribers set is done in a useEffect.
If a state update happens between these two steps (after the return value is computed but before the addition to the subscribers set is performed), the component may get a pre-update state back, and may not be re-rendered on this update.
To avoid this, a useLayoutEffect has to be used in place of the UseEffect, to make sure that the subscription happens before the state change.
How would you approach modifying the state of one store from another? Is this a sign that the states should be merged into one? Please see a contrived example below (with an obvious misuse of hooks):
const useUser = createSharedStoreHook(...);
const useCart = createSharedStoreHook({
actions: {
updateCartBasedOnUserState() {
// const [userState] = useUser();
// ...
}
},
})
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.