【问题标题】:Does not render the view after dispatch调度后不渲染视图
【发布时间】:2020-12-07 14:22:35
【问题描述】:

当我运行 Logout 函数并转到 /logout 时,我想将我的用户设置为 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;

状态提供者:

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);

【问题讨论】:

  • 你能分享你的 StateProvider.js
  • 我添加了它。 @SinanYaman

标签: reactjs react-redux react-hooks


【解决方案1】:

你可以试试

useEffect(() => {
dispatch({
        type: "SET_USER",
        user: null,
      });
},[user])

【讨论】:

  • 还是一样。它不渲染视图
  • 抱歉,之前没有看到这种方法。这是我唯一的猜测。希望你能尽快找到解决办法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-17
  • 1970-01-01
  • 2017-11-18
  • 2020-04-02
  • 1970-01-01
  • 1970-01-01
  • 2017-02-27
相关资源
最近更新 更多