"Objects are not valid as a React child. If you meant to render a collection of children, use an array instead." error

Viewed 15707

I am trying to pass a user object as the value of my React Context.Provider as in <UserContext.Provider value={user} />. However, React does not like the idea of passing objects as children. Here is the error message I get:

Error: Objects are not valid as a React child (found: object with keys {id, username, first_name, last_name, email, avatar}). If you meant to render a collection of children, use an array instead.

I hope somebody can help. Here is my React component where all this happens:

class MyApp extends App {
  constructor(props) {
    super(props);
    this.state = {
      user: {},
      authenticating: false,
    };
  }

  logout = () => {
    ...
  };

  render() {
    const { Component, pageProps } = this.props;
    const user = {
      user: this.state.user,
      logout: this.logout,
    };
    return (
      <>
        {!this.state.authenticating && (
          <UserContext.Provider value={user}>
            <Navbar />
            <Component {...pageProps} />
          </UserContext.Provider>
        )}
      </>
    );
  }
}

Interestingly, when I change

const user = {
  user: this.state.user,
  logout: this.logout,
};

to (for example)

const user = {
  username: this.state.user.username,
  logout: this.logout,
};

everything works very well.

So (as the error message above sort of implies) the problem lies in passing this.state.user to my context provider. Why? How can I solve this?

Additionally, here is my <Context.Consumer />:

  <UserContext.Consumer>
    {user => (
      <>
        {user.user ? (
          <>
            <Avatar user={user.user} />
            <Button onClick={user.logout}>Logout</Button>
          </>
        ) : (
          <>
            <Nav.Link href="/users/login">Log in</Nav.Link>
            <Nav.Link href="/users/signup">Sign up</Nav.Link>
          </>
        )}
      </>
    )}
  </UserContext.Consumer>
2 Answers

replace

function Avatar({ user }) {
  return <Nav.Link href="/users/login">{user}</Nav.Link>;
}

with

function Avatar({ user }) {
  return <Nav.Link href="/users/login">{user.something}</Nav.Link>;
}

CodeSandbox: https://codesandbox.io/s/trusting-rubin-7rcc8

It's most likely that you're trying to render the context like thus, which results in the error, rather than deconstructing the context/user object.

// this results in
// Objects are not valid as a React child (found: object with
// keys {username, age}). If you meant to render a collection of 
// children, use an array instead.

<Consumer>
{(ctx) => (
<>
 {ctx.user}
</>
)
</Consumer>

App.js

import React from "react";
import UserState, { Consumer } from "./UserState";

const Avatar = ({ user }) => (
  <>
    <div>{user.username}</div>
    <div>{user.age}</div>
  </>
);

const UserData = () => {
  return (
    <Consumer>
      {(ctx) => (
        <>
          {ctx.user && (
            <>
              <Avatar user={ctx.user} />
            </>
          )}
        </>
      )}
    </Consumer>
  );
};

export default function App() {
  const userState = {
    user: {
      username: "hi",
      age: 18
    }
  };

  return (
    <UserState.Provider value={userState}>
      <UserData />
    </UserState.Provider>
  );
}

UserState.js

import React from "react";

const UserState = React.createContext({});

export default UserState;

export const { Consumer } = UserState;
Related