我不明白为什么这个错误不是每次都出现,有时代码工作有时不工作
const dispatch = useDispatch()
const userDetails = useSelector(state => state.userDetails)
const { error, loading, user } = userDetails
const userLogin = useSelector(state => state.userLogin)
const { userInfo } = userLogin
const userUpdateProfile = useSelector(state => state.userUpdateProfile)
const { success } = userUpdateProfile
useEffect(() => {
if (!userInfo) {
**history.push**(`/login`)
}
else{
if(!user || !user.name || success){
dispatch({ type: USER_UPDATE_DETAILS_RESET })
dispatch(getUserDetails('profile'))
}
else{
setName(user.name)
setEmail(user.email)
}
}
}, [dispatch, history, userInfo, user, success ])
答案 0 :(得分:1)
在顶部添加 const history = useHistory();
。之后,您可以检查其值是否未定义。
答案 1 :(得分:-1)
import { useHistory } from "react-router-dom";
const history = useHistory();
history.push(/login
)