当我运行Logout函数并转到/ologout时,我想将我的用户设置为null并呈现注销页面,但这段代码只是设置了null,并且没有呈现我写的任何内容作为回报。问题出在哪里?
import React, { useEffect } from "react";
import { useStateValue } from "../StateProvider";
const Logout = () => {
const [{}, dispatch] = useStateValue();
dispatch({
type: "SET_USER",
user: null,
});
return (
<div>
<h1
style={{
"background-color": "#F3B858",
height: "100%",
display: "flex",
justifyContent: "center",
alignItems: "center",
height: "90vh",
color: "#fff",
"font-size": "80px",
"font-weight": 900,
}}
>
BYE
</h1>
</div>
);
};
export default Logout;
StateProvider:
import React, { createContext, useContext, useReducer } from "react";
export const StateContext = createContext();
export const StateProvider = ({ reducer, initialState, children }) => (
<StateContext.Provider value={useReducer(reducer, initialState)}>
{children}
</StateContext.Provider>
);
export const useStateValue = () => useContext(StateContext);
您可以尝试
useEffect(() => {
dispatch({
type: "SET_USER",
user: null,
});
},[user])