Replies: 1 comment 1 reply
-
As far as I know, there's no magically ideal pattern for this. You may or may not be a little more comfortable with this? interface AppState extends State {
count: number;
add: (number: number) => void;
reset: () => void;
}
const createActions = (set: SetState<AppState>) => ({
add: (number) => set((state) => ({ count: state.count + number })),
reset: () => set({ count: 0 }),
})
const useStore = create<AppState>((set) => ({
count: 0,
...createActions(set),
})); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to wrap my head around how to structure the store to provide the following features:
Initially I started with something like
To tackle the 1st point I had the functions extracted and take the state as a parameter inside
set
but it feels pretty bad ergonomically:Having the actions as functions which just work on the store as a side effect is also an option, however as far as I'm aware this completely prevents me from using something like a state creator to enable testing with custom initial data:
I'm honestly a bit lost at this point.
Beta Was this translation helpful? Give feedback.
All reactions