title: Is it possible to update a context state outside of a react component?
body: <p>In a react project, there is a globally available context for alert and messages. In any given component, I can use a context.setAlert() to put up a message.</p>
  
  <p>I have some API fetching logic extracted into a utility function. It's not a component. Components import and use this function in various ways prior to render, sort of like this:</p>
  
  <pre><code>//MyComponent.js
  import {fetchFromApi} from "../util"
  import AlertContext from "../contexts/alert"
  
  const MyComponent = () =&gt; {
    const alertContext = useContext(AlertContext)
    useEffect(() =&gt; 
      fetchFromApi(...)
        .then(() =&gt; alertContext.setAlert('All set'), [])
    return &lt;Something/&gt;
  }
  </code></pre>
  
  <p>So in fetchFromApi, I'd like to be able to use that AlertContext.setAlert method. Something like this:</p>
  
  <pre class="lang-js prettyprint-override"><code>// util.js
  export const fetchFromApi = (params) =&gt; 
    fetch("...")
      .then(something)
      .then(somethingelse)
      .then(response =&gt; {
        // set alert from the AlertContext is what I want to access
        if (response.status === 500) alertContext.setAlert('oh no')
        return response
      })
  </code></pre>
  
  <p>I'm not sure if/how I can update a context state outside of a component. Is this possible?</p>