这里是我用来通过验证身份验证传递给孩子的简单代码:
export default function PrivateRoute({ children }) {
const [BodyContent, setBodyContent] = useState("...");
const [User, setUser] = useState({});
const getBodyContent = async ({ children }) => {
const auth = await getInLocalStorage("auth");
if (auth !== "true") {
window.location.href = "/";
} else {
setBodyContent(children);
const user = await read("/auth/" + (await getInLocalStorage("userId")));
if (user.status === 200) {
setUser(user.message);
}
}
};
useEffect(() => {
getBodyContent({ children });
}, [children]);
return <Layout user={User}>{BodyContent}</Layout>;
}
如何添加是哪个用户的信息?他扮演什么角色?