简体   繁体   English

对象作为 React 子级无效(找到:object 和键 {children})。 如果您打算渲染一组孩子,请改用数组

[英]Objects are not valid as a React child (found: object with keys {children}). If you meant to render a collection of children, use an array instead

I am using context in react and having a problem to use it's props.我在反应中使用上下文并且在使用它的道具时遇到问题。 Can anyone get a solution of it.任何人都可以得到它的解决方案。 Here is my code and error.!这是我的代码和错误。! This is my useAuth.js code这是我的 useAuth.js 代码

const AuthContext = createContext();
export const AuthContextProvider = (props) => {
  const auth = Auth();
  return <AuthContext.Provider value={auth}>{props}</AuthContext.Provider>;
};


export const useAuth = () => useContext(AuthContext);
const Auth = () => {
  const [user, setUser] = useState(null);
  const provider = new firebase.auth.GoogleAuthProvider();

  const signInWithGoogle = () => {
    return firebase
      .auth()
      .signInWithPopup(provider)
      .then((res) => {
        const signedInUser = getUser(res.user);
        setUser(signedInUser);
        localStorage.setItem("hot-onion-user", true);
        return res.user;
      })
      .catch((err) => {
        console.log(err);
        setUser(null);
        return err.message;
      });
  };

And this is where I am using the context这就是我使用上下文的地方

<AuthContextProvider>
      <div className="App">
        <Router>
          <Header></Header>
          <Switch>
            <Route path="/login">
              <Login />
            </Route>
            <Route path="/signup">
              <Signup />
            </Route>
            <Route path="*">
              <NoMatch></NoMatch>
            </Route>
          </Switch>
        </Router>
      </div>
    </AuthContextProvider>

The error message is "Error: Objects are not valid as a React child (found: object with keys {children}). If you meant to render a collection of children, use an array instead."错误消息是“错误:对象作为 React 子项无效(找到:object 和键 {children})。如果您要呈现子项集合,请改用数组。”

The problem is on this line:问题出在这一行:

return <AuthContext.Provider value={auth}>{props}</AuthContext.Provider>;

Instead of props , you need to put props.children because that's the content inside the provider.您需要放置props.children而不是props ,因为这是提供程序中的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对象作为 React 子级无效(找到:object 和键 {value})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {value}). If you meant to render a collection of children, use an array instead 对象作为 React 子级无效(找到:object 和键 {weight})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {weight}). If you meant to render a collection of children, use an array instead 对象作为 React 子级无效(找到:object 和键 {_delegate})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {_delegate}). If you meant to render a collection of children, use an array instead 对象作为 React 子级无效(找到:object 和键 {arr})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {arr}). If you meant to render a collection of children, use an array instead 对象作为React子对象无效(找到:带有键{this}的对象)。 如果要渲染子级集合,请改用数组 - Objects are not valid as a React child (found: object with keys {this}). If you meant to render a collection of children, use an array instead 对象作为 React 子项无效(找到:object 和键 {totalItems})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {totalItems}). If you meant to render a collection of children, use an array instead 错误:对象作为 React 子项无效(找到:object,键为 {inputList})。 如果你打算渲染一个孩子的集合,使用一个数组 - Error: Objects are not valid as a React child (found: object with keys {inputList}). If you meant to render a collection of children, use an array 如何修复对象作为 React 子对象无效(找到:带有键 {} 的对象)。 如果您打算渲染一组子项,请改用数组 - How to fix Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead 错误:对象作为 React 子级无效(找到:object 和键 {rank})。 如果您打算渲染一组孩子,请改用数组 - Error: Objects are not valid as a React child (found: object with keys {rank}). If you meant to render a collection of children, use an array instead 错误:对象作为 React 子项无效(找到:object,键为 {})。 如果您打算渲染子集合,请改用数组。 JS - Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead. JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM