Comments (9)
I have found this middleware type useful
type Middleware<S> = (
config: StateCreator<S>,
) => (set: SetState<S>, get: GetState<S>, api: StoreApi<S>) => S;
const log: Middleware<StoreType> = config => (set, get, api) =>
config(args => {
console.log(" applying", args)
set(args)
console.log(" new state", get())
}, get, api);
from zustand.
Ah sorry, I misunderstood you. The types are exported so you can use them:
import create, { StateCreator, SetState, GetState, StoreApi } from 'zustand'
interface YourState {
a: number,
b: string
}
const log = (config: StateCreator<YourState>) => (
set: SetState<YourState>,
get: GetState<YourState>,
api: StoreApi<YourState>
) => config(args => {
console.log(" applying", args)
set(args)
console.log(" new state", get())
}, get, api)
from zustand.
@JeremyRH I agree that plain JS is very accessible to everybody. However, when writing middleware, I'm not sure how to properly type things. Therefore, I'd love an example here, even if it's not gonna make it to the doc itself.
For example:
const log = config => (set, get, api) => config(args => {
console.log(" applying", args)
set(args)
console.log(" new state", get())
}, get, api)
What are config
, set
, get
, api
types? Not that it's breaking anything, but I'd love to properly type my middleware.
Thanks!
from zustand.
I have found this middleware type useful
type Middleware<S> = ( config: StateCreator<S>, ) => (set: SetState<S>, get: GetState<S>, api: StoreApi<S>) => S; const log: Middleware<StoreType> = config => (set, get, api) => config(args => { console.log(" applying", args) set(args) console.log(" new state", get()) }, get, api);
Sorry, what is StoreType
?
from zustand.
I like that the examples are plain JS, it makes them more accessible. I also don't want to add too much noise to the readme. Maybe next to some examples we can provide a link to a typescript version using the typescript playground.
from zustand.
Thank you! This is really helpful.
from zustand.
@JeremyRH
hey !
how to write types when use it with immer ?
I defined types like:
interface IState {
currentGroup: object | null;
GroupIndex: number;
palying: boolean;
}
const myStore = (set: SetState<IState>) => ({
currentGroup: null,
GroupIndex: 0,
playing: false,
setCurrentGroup: (group: object) =>
set((state) => {
state.currentGroup = group;
})
});
const immer = (config: StateCreator<IState>) => (
set: SetState<IState>,
get: GetState<IState>,
api: StoreApi<IState>
) => config((fn) => set(produce(fn)), get, api);
export const useStore = immer(myStore);
sandbox: https://codesandbox.io/s/pedantic-flower-immerzustand-rh070?file=/src/App.tsx:196-713
from zustand.
@kurosh-z Hey, I fixed it recently. Please check the example in #169. If it doesn't work, would you please file a new issue?
from zustand.
Thank you @coffee-cup! I had to use: type Middleware<S extends State> to make it work
I have found this middleware type useful
type Middleware<S extends State> = ( config: StateCreator<S>, ) => (set: SetState<S>, get: GetState<S>, api: StoreApi<S>) => S; const log: Middleware<StoreType> = config => (set, get, api) => config(args => { console.log(" applying", args) set(args) console.log(" new state", get()) }, get, api);
from zustand.
Related Issues (20)
- 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
- overview of the zustand API HOT 1
- Persist storage : values are converted to strings before replacer
- Value is empty on initial render and sets after few milliseconds
- Immer Middelware Typescript error: Exported variable 'useMyStore' has or is using name 'WritableDraft' from external module "../node_modules/immer/dist/immer" but cannot be named.
- blacklist and whitelist
- Argument of type '(state: ClientStore) => Client | undefined' is not assignable to parameter of type 'ClientStore | Partial<ClientStore> | ((state: ClientStore) => ClientStore | Partial<ClientStore>)'. Type '(state: ClientStore) => Client | undefined' is not assignable to type '(state: ClientStore) => ClientStore | Partial<ClientStore>'. Type 'Client | undefined' is not assignable to type 'ClientStore | Partial<ClientStore>'. Type 'undefined' is not assignable to type 'ClientStore | Partial<ClientStore>'.ts(2345)
- The value I save to storage with persist is delayed when the page refreshes.
- State updated in one component but not in another one
- 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
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.