如何实现Redux DevTools?

时间:2019-03-06 15:28:26

标签: redux

这是index.js

import { createStore, applyMiddleware } from "redux";
import rootReducer from "./reducers/reducers";

const store = applyMiddleware(ReduxPromise)(createStore);

ReactDOM.render(
  <Provider store={store(rootReducer)}>
    <App />
  </Provider>,
  document.getElementById("root")
);

要使用Redux DevTools,我需要添加以下代码行:

window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()

我尝试过:

import { createStore, applyMiddleware, compose } from "redux";
import rootReducer from "./reducers/reducers";

const initialState = {};

const middleware = [ReduxPromise];

const store = createStore(
  rootReducer,
  initialState,
  compose(
    applyMiddleware(...middleware),
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
  )
);

ReactDOM.render(
  <Provider store={store(rootReducer)}>
    <App />
  </Provider>,
  document.getElementById("root")
);

但随后出现此错误: TypeError:存储不是函数

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

此行是错误的:

store(rootReducer)

您已经通过调用createStore(rootReducer) 创建了商店。如错误所述,store不是一个函数,您不能“调用”它。只需传递<Provider store={store}>

另外,请注意our new Redux Starter Kit package可以帮助您简化商店设置过程。