Trying to instanciate zustand store on class-base store

80 views Asked by At

For some reason, I'm getting undefined when I try to use zustand store. check out the code snippet

import { create } from 'zustand';

class Store {
  private readonly store;
  public use;
  private readonly setState;

  private get state() {
    return this.store.getState();
  }

  constructor() {
    this.store = create(() => ({ value: 0 }));
    this.use = this.store;
    this.setState = this.store.setState;
  }

  increment() {
    // this throw an error saying store is undefined
    this.setState((state) => ({ value: state.value + 1 }));
  }

  decrement() {
    // this throw an error saying store is undefined
    this.setState((state) => ({ value: state.value - 1 }))};
  }

export const counterStore = new Store();

I'm expecting to get the instanciated store inside my class

0

There are 0 answers