Coder Social home page Coder Social logo

Asynchronous SetState about zustand HOT 5 CLOSED

pmndrs avatar pmndrs commented on May 14, 2024 1
Asynchronous SetState

from zustand.

Comments (5)

drcmda avatar drcmda commented on May 14, 2024 3

@Magellol @JeremyRH immer can't (and won't) allow async state, but the store can still be transformed towards immer using middleware. The overhead of set remains, but at least it can mutate, and these sets then can then be async. See: https://codesandbox.io/s/5k77o8lzkn

export const immer = config => (set, get) => config(fn => set(produce(fn)), get)

const [useStore] = create(immer(set => ({
  count: 1,
  inc: async () => {
    await delay(2000)
    set(state => void state.count++)
  },
  dec: () => set(state => void state.count--)
})))

The twitter discussion where this came up: https://twitter.com/0xca0a/status/1116390665607421952

from zustand.

JeremyRH avatar JeremyRH commented on May 14, 2024 2

Hi, thanks for the feedback!

So the problem is the immer draft is stale by the time you mutate it. Even if zustand resolved promises in set, it wouldn't fix the issue because the resolved value would be stale state.

The only way I can think of solving the issue is to await the async values before calling set:

import produce from "immer";
import create from "zustand";

const fetchThing = () => Promise.resolve("thing");

const [, api] = create((set, get) => {
  const setState = fn => set(produce(get(), fn));
  return {
    things: [],
    async addThing() {
      setState(draft => {
        draft.things.push("first thing");
      });

      const asyncThing = await fetchThing();

      setState(draft => {
        draft.things.push(asyncThing);
      });
    }
  };
});

api.subscribe(console.log);
api.getState().addThing();

from zustand.

drcmda avatar drcmda commented on May 14, 2024 1

Sure it's the same thing essentially.

from zustand.

Magellol avatar Magellol commented on May 14, 2024

Thanks for the replies :)

@drcmda Just so I understand, are you saying that the method proposed by @JeremyRH right above wouldn't work either? I've tried both in codesandbox and it seems I can yield the same results (yours and Jeremy's solution). https://codesandbox.io/s/v6qno7xznl

from zustand.

Magellol avatar Magellol commented on May 14, 2024

Thanks for clarifying. I think we can close this issue then.

from zustand.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.