DEV Community

Discussion on: Can React state management get any simpler than this?

Collapse
 
skyjur profile image
Ski • Edited

Don't think of an entity as a class that you need to "instantiate".

It's a very useful thought though - as every variable needs to instantiated with initial state. If it's class or not doesn't matter at all.

What I see here in your examples is completely no different of just using useState(). Thus I completely miss the point that you're trying to achieve.

const useCounterStore = () => {
  const [counter, setCounter]= useState()
  return { 
      counter,
      increment(by) {
         setCounter(counter+by)
      },
      reset() {
         setCounter(0)
      }
  }
}

function SomeComponent() {
   const counterA = useCounterStore()
   const counterB = useCounterStore()
   const counterC = useCounterStore()
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
arnelenero profile image
Arnel Enero

Your example would not be "shared state", is it?