title: What am I doing wrong with React context?
body: <p>Quick context, I am making a character-building app (think D&amp;D Beyond). Users can log in, use the builder to generate a character, and then later recall and level-up that character. I'm hosting the data on MongoDB and using Realm Web for CRUD on the database. Based on <a href="https://blog.shahednasser.com/how-to-integrate-mongo-realm-with-react-part-1/#react-setup" rel="nofollow noreferrer">this guide</a>, I'm using React Context to keep track of the logged-in user as they navigate the site. I'm trying to create a widget in the NavMenu that will allow me to see the logged-in user for dev purposes (later, I can make a &quot;Welcome, [name]!&quot;-type greeting), but I keep getting the error that &quot;mongoContext.user&quot; is null. This makes sense, as the Context is set to null with useState, but as soon as the page renders there is an init() function which anonymously logs in the user if they aren't already. I can't seem to understand why this is happening, even after looking at 4 different articles on how to use React Context. Code Snippets below:</p>
  <p><strong>App.js</strong></p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>  const [client, setClient] = useState(null);
    const [user, setUser] = useState(null);
    const [app, setApp] = useState(new Realm.App({id: process.env.REACT_APP_REALM_APP_ID}));
    
    //Anonymously logs in to mongoDB App instance on page render
    useEffect(() =&gt; {
      const init = async () =&gt; {
        if (!user) {
          setUser(app.currentUser ? app.currentUser : await app.logIn(Realm.Credentials.anonymous()));
        }
        if (!client) {
          setClient(app.currentUser.mongoClient('mongodb-atlas'));
        }
      }
    
      init();
    }, [app, client, user]);
    
    //Function to render a given component and pass it the mongoContext prop
    const renderComponent = (Component, additionalProps = {}) =&gt; {
      return (
      &lt;MongoContext.Consumer&gt;
        {(mongoContext) =&gt; &lt;Component mongoContext={mongoContext} {...additionalProps} /&gt;}
      &lt;/MongoContext.Consumer&gt;
      );
    }
  
    return (
      &lt;MongoContext.Provider value={{app, client, user, setClient, setUser, setApp}}&gt;
        &lt;Layout&gt;
          &lt;Switch&gt;
            &lt;Route exact path="/" render={() =&gt; renderComponent(Home)} /&gt;
            ... other Routes ...
          &lt;/Switch&gt;
        &lt;/Layout&gt;
      &lt;/MongoContext.Provider&gt;
    );
  }</code></pre>
  </div>
  </div>
  </p>
  <p>As outlined in the guide, each Route receives a render function which wraps it in the Context.Consumer component and sends context. I couldn't figure out if this was possible with the Layout component (since it is itself a component wrapper), so instead I used React.useContext() inside the component I need context for: (the component relationship is Layout -&gt; NavMenu -&gt; UserDetail)</p>
  <p><strong>UserDetail.jsx</strong>
  <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const UserDetail = (props) =&gt; {
      const mongoContext = React.useContext(MongoContext);
  
      return (
              &lt;div&gt;
                  &lt;h1&gt;Logged in as: {mongoContext.user.id}&lt;/h1&gt;
              &lt;/div&gt;
      );
  }</code></pre>
  </div>
  </div>
  </p>
  <p>This is the component which gives the &quot;mongoContext.user is null&quot; error. The other components seem to use context just fine, as I have done some Create operations with the anonymous user. I can see that it probably has something to do with how the other components are rendered with the Consumer wrapper, but according to every guide I've read, the React.useContext in the UserDetail component should work as well. Thanks for any help, this has completely stymied the project for now.</p>