question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

typing problem storeApi outside of react with immer middleware

See original GitHub issue

@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?

_Originally posted by @dai-shi in https://github.com/react-spring/zustand/issues/75#issuecomment-684075606_

Hi , #169 actually solved the typing problem inside of react. But if you use the setState api outside of react, typescript requires you to return the state.

// outside of react
useStore.setState((state) => {
  state.bb.b1 = true;
});
Screen Shot 2020-09-01 at 3 21 31 PM

codeSandbox :

https://codesandbox.io/s/pedantic-flower-immerzustand-forked-ptso7?file=/src/App.tsx

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, Sep 10, 2020

Let me close this issue as the first question is answered and now the context is a bit changed.

I added the above immer snippet in readme.md.

0reactions
dai-shicommented, Sep 10, 2020

Ah, the codesandbox example in my comment. Here’s the fixed version: https://codesandbox.io/s/pedantic-flower-immerzustand-forked-5zdy9

The immer middleware with types:

const immer = <T extends State>(
  config: StateCreator<T, (fn: (state: T) => void) => void>
): StateCreator<T> => (set, get, api) =>
  config((fn) => set(produce(fn) as (state: T) => T), get, api);

Very weird the you need to extend it

It’s a long story (also discussed in various TS issues), but that’s how it works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding persist middleware to zustand store - Stack Overflow
https://github.com/pmndrs/zustand#middleware. you can find it under How to pipe middlewares const createStore = pipe(persistor, devtools, ...
Read more >
Persist middleware - Zustand Documentation
The persist middleware enables you to store your Zustand state in a storage (e.g. localStorage , AsyncStorage , IndexedDB , etc.
Read more >
Redux Essentials, Part 8: RTK Query Advanced Patterns
If we want to fetch the list of users outside of React, ... The actual caching reducer and middleware that we originally added...
Read more >
Simplify and standardize your Redux configuration with Redux ...
Are you using Redux to manage state in your React application, ... using the Immer library; Support for async thunk out of the...
Read more >
Stop Overcomplicating your State – Try Zustand
... Equality Middleware Store... Tagged with zustand, webdev, react, redux. ... We can create middleware to allow us to use immer easily.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found