im尝试创建带有标头值的api请求,该标头值是从上下文组件接收的。但是,页面组件一加载,它就会引发Cannot read property '_id' of null
异常。加载上下文后,是否可以运行useEffect函数?
主要组件:
import React, { useState, useEffect, useContext } from "react";
import "./overview.scss";
/* COMPONENTS */';
import axios from 'axios';
import { GlobalContext } from '../../components/context/global';
const Overview = () => {
const [bookings, setBookings] = useState([]);
const [loaded, setLoaded] = useState(false);
const [user, setUser] = useContext(GlobalContext);
useEffect(() => {
axios
.get(`/api/v1/bookings/user/${user._id}`)
.then(res => setBookings(res.data))
.catch(err => console.log(err))
.finally(() => setLoaded(true));
}, [user]);
上下文组件:
import React, {useState, useEffect, createContext} from 'react';
import jwt from 'jsonwebtoken';
/* GLOBAL VARIABLES (CLIENT) */
export const GlobalContext = createContext();
export const GlobalProvider = props => {
/* ENVIRONMENT API URL */
const [user, setUser] = useState([]);
useEffect(() => {
const getSession = async () => {
const user = await sessionStorage.getItem('authorization');
setUser(jwt.decode(user));
}
getSession();
}, [])
return (
<GlobalContext.Provider value={[user, setUser]}>
{props.children}
</GlobalContext.Provider>
);
};
答案 0 :(得分:2)
这里的问题是useEffect
正在安装,而您还没有用户。您只需要防止这种情况发生
useEffect(() => {
if (!user) return;
// use user._id
},[user])
自然,当上下文获取用户时,它应强制重新渲染您的组件,而useEffect
自然应该在依赖关系发生变化时重新运行。
答案 1 :(得分:0)
在渲染GlobalProvider之前先输入条件,例如:
return (
{user.length&&<GlobalContext.Provider value={[user, setUser]}>
{props.children}
</GlobalContext.Provider>}
);
如果用户不是数组,请使用此
return (
{user&&<GlobalContext.Provider value={[user, setUser]}>
{props.children}
</GlobalContext.Provider>}
);