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.
useState()
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() }
Your example would not be "shared state", is it?
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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.Your example would not be "shared state", is it?