【问题标题】:Redux extension not showing storeRedux 扩展不显示商店
【发布时间】:2020-08-18 14:00:02
【问题描述】:

我正在学习 redux,为了调试,我在 Chrome 中安装了他们的调试扩展。除了我的代码不起作用之外,为什么扩展程序不显示存储数据,尽管后者已由 const store = createStore(counter); 初始化?

index.js

import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import { createStore } from "redux";
import App from "./App";

const store = createStore(counter);

function counter(state = 0, action) {
  switch (action.type) {
    case "INCREMENT":
      return state + 1;
    case "DECREMENT":
      return state - 1;
    default:
      return state;
  }
}
function App() {
  return (
    <div>
      <p>{store.getState()}</p>
      <button onClick={() => store.dispatch({ type: "INCREMENT" })}>
        Increment
      </button>
    </div>
  );
}
ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById("root")
);

Stackblitz:https://stackblitz.com/edit/react-p8vm7h?file=src/index.js

提前谢谢你!

【问题讨论】:

    标签: reactjs redux


    【解决方案1】:

    因为the store needs to be configured so that the extension can listen for changes and read its state。您的代码只是自己创建了一个普通存储,没有任何 DevTools 连接设置。

    您应该从 our official Redux Toolkit package 切换到使用 the configureStore API,这是我们推荐的编写 Redux 逻辑的方法。 configureStore 自动设置商店以使用 DevTools 扩展,并添加额外的检查以防止意外突变等常见错误。

    【讨论】:

      【解决方案2】:

      查看创建者本身的 GitHub 页面。它很好地解释了如何连接 Store 和扩展。如果您需要更多信息,请在 cmets 中告诉我

      【讨论】:

        猜你喜欢
        • 2021-12-10
        • 1970-01-01
        • 1970-01-01
        • 2012-06-07
        • 1970-01-01
        • 2021-07-14
        • 2017-10-08
        • 1970-01-01
        相关资源
        最近更新 更多