gpt4 book ai didi

javascript - 使用 redux devtools(chrome 扩展)配置 redux store

转载 作者:行者123 更新时间:2023-11-29 21:24:46 29 4
gpt4 key购买 nike

我正在使用以下样板示例,并尝试将其配置为与 redux devtools 的 chrome 扩展一起使用:

import { createStore, applyMiddleware } from 'redux'
import thunkMiddleware from 'redux-thunk'
import apiMiddleware from '../middleware/api'
import createLogger from 'redux-logger'
import rootReducer from '../reducers'


const logger = createLogger({
level: 'info',
collapsed: false,
logger: console,
predicate: (getState, action) => true
})

const createStoreWithMiddleware = applyMiddleware(
thunkMiddleware,
apiMiddleware,
logger
)(createStore)

export default function configureStore(initialState) {
const store = createStoreWithMiddleware(rootReducer, initialState)

if (module.hot) {
// Enable Webpack hot module replacement for reducers
module.hot.accept('../reducers', () => {
const nextRootReducer = require('../reducers')
store.replaceReducer(nextRootReducer)
})
}

return store

我试过按如下方式添加它,但出现窗口未定义错误:

import { compose, createStore, applyMiddleware } from 'redux'

const createStoreWithMiddleware = compose(applyMiddleware(
thunkMiddleware,
apiMiddleware,
logger
),window.devToolsExtension ? window.devToolsExtension() : f => f)(createStore)

我假设结构与 reudx-devtools 扩展示例页面中给出的结构有所不同,但我无法确定。

如何使用中间件和增强功能以​​正确的方式设置商店?

最佳答案

typeof window === 'object' && typeof window.devToolsExtension !== 'undefined'
? window.devToolsExtension()
: f => f

这应该可以修复错误。

关于javascript - 使用 redux devtools(chrome 扩展)配置 redux store,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37764771/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com