gpt4 book ai didi

redux - 如何将状态 sanitizer 与 React-Redux 中的现有中间件结合起来

转载 作者:行者123 更新时间:2023-12-05 05:06:32 25 4
gpt4 key购买 nike

我的 redux store 相当大; Redux Devtools 建议清理较大的对象以提高性能。

我已经按照此处的文档进行操作:https://github.com/zalmoxisus/redux-devtools-extension/blob/master/docs/Troubleshooting.md#excessive-use-of-memory-and-cpu

我在这里尝试了多种组合,但都没有给我预期的输出。

当前版本,如下所示,导致状态作为函数而不是对象返回。我知道我做错了什么,但我不确定是什么。任何指导将不胜感激。

这是我的 store.js:


'use strict'
// libraries
import { createStore, applyMiddleware, compose } from 'redux'

// middleware
import logger from 'redux-logger'
import thunk from 'redux-thunk'

// reducers
import reducer from './reducers'

const withLogger = false ? (thunk, logger) : thunk

const isProd = process.env.NODE_ENV === 'production'

const middleware = isProd ? thunk : withLogger

const composeEnhancers = isProd
? compose
: window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose

// sanitizers to keep redux devtools from using excessive memory
const actionSanitizer = action =>
!!action.id
&& action.type === `RECEIVE_${action.id.toString().toUpperCase()}_COLLECTION`
? { ...action, data: '<<LONG_BLOB>>' }
: action

const store = createStore(
reducer,
composeEnhancers(applyMiddleware(middleware)),


// The addition of this code breaks my store

window.__REDUX_DEVTOOLS_EXTENSION__
&& window.__REDUX_DEVTOOLS_EXTENSION__({
actionSanitizer,
stateSanitizer: state =>
state.data ? { ...state, data: '<<LONG_BLOB>>' } : state
})

// End breaking code

)

第二次尝试

我已经进行了一些更新,现在可以在 devtools 中看到 sanitizer 的效果 - 取决于我在 createStore 函数中的位置。不幸的是,这改变了我的 composeEnhancers 行为(根据位置触发或不触发)


// middleware with or without logger
const middlewareEnhancer =
true || ENV === 'production' // change to false to prevent logger output
? applyMiddleware(thunk, logger)
: applyMiddleware(thunk)

// sanitizers to keep redux devtools from using excessive memory
const actionSanitizer = action =>
!!action.id
&& action.type === `RECEIVE_${action.id.toString().toUpperCase()}_COLLECTION`
? { ...action, data: '<<LONG_BLOB>>' }
: action

// compose
const composeEnhancers =
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__(middlewareEnhancer) ||
compose(middlewareEnhancer)

const store = createStore(
// createStore signature > reducer, preLoadedState, enhancer
rootReducer,
// devtools extension works when I place it here per the examples in docs
// BUT composed enhancers fail
// Obviously, since the format wouldn't match the createStore signature
// I have no idea how `__REDUX_DEVTOOLS_EXTENSION__` should be used in conjunction with composeEnhancers

undefined,
composeEnhancers,

// devtools extension fails when placed here
// composed enhancers run

window.__REDUX_DEVTOOLS_EXTENSION__
&& window.__REDUX_DEVTOOLS_EXTENSION__({
actionSanitizer,
stateSanitizer: state =>
state.data ? { ...state, data: '<<LONG_BLOB>>' } : state
})

)

最后,坚持!

我讨厌放弃;在重新阅读@markerikson 发布的所有文档后弄明白了。始终阅读文档 :'(

这可能对任何使用 configureStoreRedux Toolkit 的人都没有用,但无论如何我都会记录它。

我最大的错误是 actionSanitizerstateSanitizer 是 Devtools Extension 选项,应该这样添加。觉得自己很傻,但至少我不会忘记。

唯一剩下要做的就是实现 redux-devtools-extension避免按照 markerikson 的建议使用 window.__SOMEFUNC__

实际解决方案:

'use strict'
// libraries
import { createStore, applyMiddleware, compose } from 'redux'

// middleware
import logger from 'redux-logger'
import thunk from 'redux-thunk'

// reducers
import rootReducer from './reducers'

// middleware with or without logger
const middlewareEnhancer =
true || ENV === 'production' // change to false to prevent logger output
? applyMiddleware(thunk, logger)
: applyMiddleware(thunk)

// sanitizers to keep redux devtools from using excessive memory
const actionSanitizer = action =>
!!action.id
&& action.type === `RECEIVE_${action.id.toString().toUpperCase()}_COLLECTION`
? { ...action, data: '<<LONG_BLOB>>' }
: action

// compose
const composeEnhancers =
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({

// add sanitizers here as devtools options
// see https://github.com/zalmoxisus/redux-devtools-extension/tree/94f7e53800f4665bddc9b7438c5cc75cfb4547cc#12-advanced-store-setup
// section 1.2

actionSanitizer,
stateSanitizer: state =>
state.data ? { ...state, data: '<<LONG_BLOB>>' } : state
}) || compose

const enhancer = composeEnhancers(middlewareEnhancer)

const store = createStore(rootReducer, undefined, enhancer)

export default store

最佳答案

作为第一个观察,这条线似乎是错误的:

const withLogger = false ? (thunk,记录器):thunk

我强烈建议您首先切换到使用 the configureStore function from our official Redux Toolkit package ,它会为您处理商店设置过程。如果需要,您仍然可以从那里将 DevTools 配置选项传递给 configureStore()

关于redux - 如何将状态 sanitizer 与 React-Redux 中的现有中间件结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59826789/

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