Comments (6)
I'm thinking more if someone already had a hook written.
function useMyState() {
const [state, dispatch] = userReducer(...)
const actions = {...}
// maybe some more logic
return someApi
}
Then they could just make it "shared" state, meaning it could be used in multiple components. Like a higher-order hook.
export useMySharedState = share(useMyState)
Anyway, sorry to beat a dead horse :) THANK YOU for this library, we've started using it in our startup's site. It's going well so far!
from zustand.
This would be nice but I don't know if your example is possible. State is shared without using a provider by collecting the setState
and dispatch
functions in a list and calling them when needed. It is possible for someone to create a package that does this though:
import { useReducer, useState } from 'react';
import share from 'react-share-state';
export const useSharedReducer = share(useReducer);
export const useSharedState = share(useState);
from zustand.
well it doesn't need a provider because it's not context based. these hooks work everywhere, even outside of react bounds, like reconcilers. the hook you create already is a shared connector, that can be used anywhere in your app.
or maybe i don't understand it yet, could you go into shared state a little more deeper?
from zustand.
Sorry, let me try to explain again.
Your library is two things to me:
- A way to share state with many components without using Provider
- An elegant state API
1 is a worthwhile discovery on its own. 2 is also good because you value simple APIs. Each could have been built independently, with 2 being built using 1.
But what if someone loves reducers or something else? I know you allow middleware for API customization, but your lib still has opinions. It would be cool if there was a lower-level share
higher-order hook that converted any state-based hook to a "shared" state-based hook, without the need to use a Provider. Then your opinionated API would be built on top of this share
hook, as a higher-level API.
from zustand.
reducers are no problem though
const types {
increase: "INCREASE",
decrease: "DECREASE"
}
const reducer = (state, { type, ...payload }) => {
switch (type) {
case types.increase: return { ...state, count: state.count + 1 }
case types.decrease: return { ...state, count: state.count - 1 }
}
return state
}
const [useStore] = create((set, get) => ({
count: 0,
dispatch: args => set(state => reducer(state, args)),
})
Maybe give me some code examples, need to see it to imagine how it works.
from zustand.
I'm going to close this for now as it seems like this is something another library could solve.
from zustand.
Related Issues (20)
- The reactNode is stored in the store, sometimes causing the component to lose update. HOT 1
- Is there a way to reset data when switching routes? The previous method cannot take effect#1192
- Type problem with persist HOT 3
- Reset store not working on log out
- I can't update state with new data from Tanstack Query HOT 2
- The app hanging when initializing the store with and array of objects. HOT 4
- Is there any chance to simplify types?
- How to move the router state to store?
- Website graphics rendering issues (Linux) HOT 3
- shallow not support nested object HOT 1
- Are these two methods exactly the same? HOT 1
- Itβs not recommended to suspend a render based on a store value returned by useSyncExternalStore.
- RFC: How is v5-alpha? HOT 11
- should useShallow add into the documents?
- Can't use persist with context implementation mentioned in the docs for Next.js HOT 1
- Re-render issue while performing open and setOpen using shadcn
- Re-render issue while performing open and setOpen using shadcn HOT 1
- type WithoutCallSignature<T>
- docs: Add suggestions about getInitialState on persist
- Cant user Array of Map type to store data
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zustand.