title: React global state no context or redux?
body: <p>I recently game across the following article <a href="https://medium.com/javascript-in-plain-english/state-management-with-react-hooks-no-redux-or-context-api-8b3035ceecf8" rel="nofollow noreferrer">State Management with React Hooks — No Redux or Context API</a>.  Since reacts inception the most talked about issue is always state management and global state.  Redux has been the popular choice and more recently the context API.  But this approach seems to be much easier, less code and more scalable.</p>
  <p>My question is can anyone see a down side to using the this type of state management approach that I may have overlooked.  I have tweeked the code a little to support SSR and it works in Nextjs and also made it a little more friendly to use actions and the setting of the state variable.</p>
  <p>useGlobalState.js</p>
  <pre><code>import React, { useState, useEffect, useLayoutEffect } from 'react';
  
  const effect = typeof window === 'undefined' ? useEffect : useLayoutEffect;
  
  function setState(newState) {
      if (newState === this.state) return;
      this.state = newState;
      this.listeners.forEach((listener) =&gt; {
          listener(this.state);
      });
  }
  
  function useCustom() {
      const newListener = useState()[1];
      effect(() =&gt; {
          this.listeners.push(newListener);
          return () =&gt; {
              this.listeners = this.listeners.filter((listener) =&gt; listener !== newListener);
          };
      }, []);
      return [this.state, this.setState, this.actions];
  }
  
  function associateActions(store, actions) {
      const associatedActions = {};
      if (actions) {
          Object.keys(actions).forEach((key) =&gt; {
              if (typeof actions[key] === 'function') {
                  associatedActions[key] = actions[key].bind(null, store);
              }
              if (typeof actions[key] === 'object') {
                  associatedActions[key] = associateActions(store, actions[key]);
              }
          });
      }
      return associatedActions;
  }
  
  const useGlobalHook = (initialState, actions) =&gt; {
      const store = { state: initialState, listeners: [] };
      store.setState = setState.bind(store);
      store.actions = associateActions(store, actions);
      return useCustom.bind(store, React);
  };
  
  export default useGlobalHook;
  </code></pre>
  <p>Then set up a custom hook for a state variable can be a simple string or a object here is a simple one:</p>
  <pre><code>import useGlobalState from './useGlobalState';
  
  const initialState = 'Hi';
  
  // Example action for complex processes setState will be passed to component for use as well
  const someAction = (store, val) =&gt; store.setState(val); 
  
  const useValue = useGlobalState(initialState, { someAction });
  
  export default useValue;
  </code></pre>
  <p>And use in component:</p>
  <pre><code>import React from 'react'
  import useVal from './useVal'
  
  export default () =&gt; {
    const [val, setVal, actions] = useVal();
  
    const handleClick = () =&gt; {
      setVal('New Val');
      // or use some actions
      actions.someAction('New Val');
    }
  
    return(
      &lt;div&gt;{val}&lt;/div&gt;
      &lt;button onClick={handleClick}&gt;Click Me&lt;/button&gt;
    )
  }
  </code></pre>
  <p>This all seems like a much cleaner and easier approach and I am wondering why this isn't the go to approach for state management in react.  First you don't have to wrap everything in a provider.  Next it is extremely easy to implement and much less code is involved in the actual app.  Can anyone see a downside to using this approach. The only thing I can think of is the re rendering issue that the context api has but in small chunks this shouldn't be an issue.</p>