Comments (8)
you should be able to set the initialState variable
const initialState = { ... }
const [useStore, api] = create(set => initialState)
api.setState(initialState)
from zustand.
@laurib Unfortunately, Zustand's set
doesn't allow this right now. I guess the best option is to store all your state in a property and overwrite that property. That's kind of messy but I think a create
wrapper can abstract this for you:
import create from "zustand";
function createNoMerge(stateCreator) {
let setState;
let getState;
const [useStore, api] = create((set, get) => {
setState = newState => {
const oldState = get().state;
const state =
typeof newState === "function"
? newState(oldState)
: newState;
return set({ state });
};
getState = () => get().state;
return {
state: stateCreator(setState, getState)
};
});
api.setState = setState;
api.getState = getState;
const useStoreNoMerge = (sel = s => s, ...args) => {
const selector = () => sel(getState());
return useStore(selector, ...args);
};
return [useStoreNoMerge, api];
}
const [useStore, api] = createNoMerge(() => ({
a: 1,
b: 2
}));
from zustand.
Are there any plans to change set
to support this?
Thanks in advance!
from zustand.
@MrFungusEli I would like the default behavior of set
to overwrite instead of merge. That would be a breaking change though.
from zustand.
wouldnt overwrite require full reducers though? like when you just want to change a single value.
state { a: 1, b: 2, c: 3 }
set({ a: 0 }) // ---> { a: 0 } ?
that would make set(value) quite useless except for resets, since you would always need the function form: set(state => ...).
from zustand.
Is there anything I can do to help? I'm wondering if this would be a good first time contribution for me :)
from zustand.
i guess making it merge or not is a small change, but the repercussions of how it effects all purposes and use cases isn't clear to me. from my experience, resetting a set of objects is rare, i solve it via intialState. plain deletion i haven't ran into. merging, or set(value) instead of set(function) on the other hand is something i do often - i realize this could just be me.
if the change would make set(value) obsolete because it would now delete all data except the one prop that it gets, and set(function) would always have to spread, then i think it needs a bigger discussion involving more users. we should also look into what made react go for setState and useState as a merging op.
alternatives would be middleware or set(arg, merge=true)
from zustand.
you should be able to set the initialState variable
const initialState = { ... } const [useStore, api] = create(set => initialState) api.setState(initialState)
I must have missed this, this works absolutely fine for me. Thanks!
from zustand.
Related Issues (20)
- Typescript + Slices = trouble HOT 7
- Setting nested state make the other states change (their values didn't change)
- zustand concurrent update issue
- Typo in GitHub "About" HOT 1
- import { create ,WritableDraft} from "zustand" HOT 1
- Introduction Zustand page a documentation typo HOT 1
- Ts error on type annotation HOT 3
- Persist middleware keeping old versions of functions around. Breaking apps when deploying to prod
- devtools with option `serialize.immutable` does not deserialize state from devtools correctly (broken redux devtools "Jump" / "Revert" etc.)
- state empty on first render HOT 1
- onRehydrateStorage is invoked before storeage.getItem HOT 1
- About API
- About api docs
- Documentation incorrect: Usage with React Contexts HOT 2
- "useEffect" is not exported by "node_modules/react/index.js"
- Usage of import.meta.env
- enums not working HOT 4
- help wanted
- Search does not work on docs WWW HOT 2
- State not updating in the component
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.