Exploring the limitations of module state
Because module state resides outside React components, there's a limitation: the module state defined globally is a singleton, and you can't have different states for different component trees or subtrees.
Let's revisit our createStore
implementation from Chapter 4, Sharing Module State with Subscription:
const createStore = (initialState) => { let state = initialState; const callbacks = new Set(); const getState = () => state; const setState = (nextState) => { state = typeof nextState === 'function' ? nextState(state) : nextState; callbacks.forEach((callback) => callback()); }; const subscribe =(callback) => { callbacks.add(callback); return () => { callbacks.delete(callback); }; ...