[英]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.