title: Why Is React Context.Provider Necessary (Or useful)?
body: <p>The reason React has contexts is to allow for multiple sibling components to share a piece of state-data. It is the go-to method for allowing two unrelated components to read/write in shared variables. The reason it is necessary is that React has no way to easily source a data value to multiple screens without actually passing that data between screens. Instead, it allows each screen access to the data when it needs it.</p>
  
  <p>So... The implementation requires that a component be created, called a <code>Context.Provider</code> component, and then you have to wrap the components who need access to the shared data inside the <code>Context.Provider</code>. But why? Why on earth is that a requirement? <code>Context</code>s are designed sharing data between components who aren't hierarchally related, and were required to put the components within a heirarchy to do so?</p>
  
  <p>It would be 100 times more straight forward and just as effective to simply drop the requirement of using a Context.Provider, simple have the <code>useContext</code> function give access to a set variable by default:</p>
  
  <pre><code>// In ctx.js
  import React from 'react';
  export default CTX = React.createContext({val: "value"});
  </code></pre>
  
  <pre><code>// In compA.js
  import CTX from './ctx.js';
  import {useContext} from 'react';
  function A(props) {
      var [context, setContext] = useContext(CTX);
      console.log(context); //Logs {val: 'value'};
      setContext({val: "newValue"});
  }
  </code></pre>
  
  <p>Then later on, assuming component B renders after A:</p>
  
  <pre><code>import CTX from './ctx.js';
  import {useContext} from 'react';
  function B(props) {
      var [context, setContext] = useContext(CTX);
      console.log(context); //Logs {val: 'newValue'};
  }
  </code></pre>
  
  <p>The above usage, if it actually worked, solves the task of "sharing data between unrelated components", and is much much simpler than requiring an entire new component be defined in the context file. This solution is better because:
  1. No required restructuring of the application. You don't need to wrap components in a provider. 
  2. Any Components can just ask for any shared state easily, and they can set the shared state easily.
  3. Easier to understand with much less code involved (One line of code for import and one line to initiate the context).
  4. Doesn't sacrifice anything. This method allows for easy sharing of state between components, which is the entire reason of contexts in the first place.</p>
  
  <p>Am I crazy? Is there a legitamate reason that we'd absolutely need to wrap our components up in a special component to share data?.. Why can't the shared state just exist independently? Its like they chose a bad solution... Why make every developer wrap there components in another component before using shared state, why not just let the developer use the damned shared state when they need to use it instead of jumping through a hoop? Someone please educate me.</p>
  
  <p>Edit: One answer said that with my described method we wouldn't be able to access multiple contexts with a single component. That is false. It is actually easier with my described method:</p>
  
  <pre><code>// In context.js
  export const CTX = React.createContext({val: "val"});
  export const CTX2 = React.createContext({val2: "val2"});
  </code></pre>
  
  <pre><code>// In app.js
  
  function App(props) {
      const [state, setState] = useContext(CTX);
      const [state2, setState2] = userContext(CTX2);
      return (&lt;&gt;&lt;/&gt;);
  }
  </code></pre>
  
  <p>Easy. No need for <code>Context.Provider</code>. This is multiple contexts being used in one component, requiring just two calls to useContext versus wrapping your entire application in two nested contexts, which is what is what you have to do with current <code>Context.Provider</code> method...</p>