【问题标题】:Redux State Does Not Get Updated In Dev Tools But Current State Is Being Reflected In PropsRedux 状态不会在开发工具中更新,但当前状态会反映在道具中
【发布时间】:2022-01-14 16:12:11
【问题描述】:

我是一个 redux 菜鸟,想了解 redux 的工作原理。 所以很简单。

我有一家商店

我有一个 reducer,它只返回一个键/对值 {authenticated:false}

我在子组件中有一个 useEffect 函数,只要 props 发生变化,console.log 就是 props 值。

子组件以某种方式记录更新的道具,只要它发生更改,但更改不会反映在 redux 开发工具中。

下面是我的代码

index.js

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { Provider } from "react-redux";
import ReduxStore from "./ReduxStore/store";
ReactDOM.render(
  <React.StrictMode>
    <Provider store={ReduxStore}>
      <App />
    </Provider>
  </React.StrictMode>,
  document.getElementById("root")
);

App.js

import { connect, useDispatch } from "react-redux";
const { React, useEffect } = require("react");
const App = (props) => {
  const dispatch = useDispatch();
  useEffect(() => {
   //PS: i intentionally ignored the action dict inside the reducer
    dispatch({ type: "AppReducer", payload: { type: "checkUserToken" } });
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  useEffect(() => {
    console.log("Props Changed ", props);
// This code above works because it returns the state returned from the reducer anytime it changes
  }, [props]);
  return <>hola </>;
};
const matchStateToProps = (state) => ({ state });
export default connect(matchStateToProps)(App);

store.js

const RootReducer = (state = {}, action) => {
  return {
    authenticated: false,
  };
  
};

const ReduxStore = createStore(RootReducer);
ReduxStore.subscribe(() => console.log("State Is ", ReduxStore.getState()));
export default ReduxStore;

【问题讨论】:

    标签: reactjs redux react-redux


    【解决方案1】:

    我找到了问题的答案。

    唯一缺少的代码行是添加

    window.__REDUX_DEVTOOLS_EXTENSION__ &amp;&amp; window.__REDUX_DEVTOOLS_EXTENSION__() createStore 参数

    代码应该是

    const ReduxStore = createStore(
      RootReducer,
      window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
    );
    

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 2019-12-11
      • 1970-01-01
      • 2020-11-10
      • 2020-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-20
      相关资源
      最近更新 更多