Giter VIP home page Giter VIP logo

Comments (6)

tafelito avatar tafelito commented on May 5, 2024 8

@drcmda if you use a callback, your component still rendering twice. Once for the initial value and then after you call the init, right?

And that would the the same as using useEffect

const { value, setValue } = useStore();
useEffect(() => {
    setValue(initValue);
  }, [initValue]);

Btw, cool game!

from zustand.

drcmda avatar drcmda commented on May 5, 2024 2
function A(props) {
  const [[useStore]] = useState(() => create(set => ({ a: props.a }))

i usually use a callback though, an init() function that i call after components have been created. here's a recent example: https://codesandbox.io/s/react-three-fiber-untitled-game-i2160 (down there where the canvas component it, needed the store to be global, but relies on the camera, which is created later).

from zustand.

dai-shi avatar dai-shi commented on May 5, 2024 1

Look like resolved. Please file a new issue for further discussion.

from zustand.

drcmda avatar drcmda commented on May 5, 2024

yes, that would add one render.

from zustand.

tafelito avatar tafelito commented on May 5, 2024

creating the store inside the components state won't allow you to have an initial state. Once the component is unmounted the state is destroyed as does the store. So basically putting the store in state is the same as just using the state by itself

from zustand.

baybara-pavel avatar baybara-pavel commented on May 5, 2024

@joelmoss, you can do it in that way too

const [useStore, api] = create(set => ({
  a: "dafaultState",
  set: fn => set(produce(fn))
}));

const ExampleComponent = ({ a }) => {
  const initialState = { a };
  const ref = useRef(initialState);
  const state = ref.current;
  const store = useStore();

  useEffect(
    () =>
      api.subscribe(
        state => {
          if (state) {
            ref.current = state;
          }
        },
        state => state
      ),
    []
  );

  return (
    <div
      onClick={() =>
        store.set(state => {
          state.a = "nextState";
        })
      }
    >
      Test: {state.a}
    </div>
  );
};

https://codesandbox.io/s/elastic-sutherland-r43x5

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.