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

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

我正在尝试将 user 对象作为我的 React Context.Provider 的值传递给 <UserContext.Provider value={user} />。然而,React 不喜欢将对象作为子对象传递的想法。这是我收到的错误消息:

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.

我希望有人能提供帮助。这是我的 React 组件,其中发生了所有这些:

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>
        )}
      </>
    );
  }
}

有趣的是,当我改变

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

至(例如)

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

一切正常。

所以(正如上面的错误消息所暗示的那样)问题在于将 this.state.user 传递给我的上下文提供程序。为什么?我该如何解决?

此外,这是我的 <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>

替换

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

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

代码沙箱: https://codesandbox.io/s/trusting-rubin-7rcc8

很可能您正试图像这样渲染上下文,这会导致错误,而不是解构 context/user 对象。

// 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;