【问题标题】:Javascript - CreateStore - Passing multiple functions as only one parameter - Redux - React NativeJavascript - CreateStore - 仅将多个函数作为一个参数传递 - Redux - React Native
【发布时间】:2020-05-13 09:01:39
【问题描述】:

我想将多个增强器函数传递给 Redux 的 createStore 函数。

这种方法没有奏效:

const store = createStore(rootReducer, [composeWithDevTools(), applyMiddleware()]); // !!!!! Remove composeWith Dev FOR RELEASE

当我只通过 DevTools 增强器时,这是有效的:

const store = createStore(rootReducer, composeWithDevTools());

我正在与 React Native 合作以获取信息。 如何将多个 Enhancer 传递给 CreateStore 函数? 谢谢你的帮助

编辑: - 已解决

经过一些尝试并获得了一些关于作曲家的知识(感谢回答者的提示),我找到了正确的用法:

const store = createStore(rootReducer, composeWithDevTools(applyMiddleware(ReduxThunk)));

【问题讨论】:

    标签: javascript node.js reactjs react-native redux


    【解决方案1】:

    您似乎想要来自 redux 的 compose 函数。这是一个包含许多增强器的示例,也记录在 in the Redux "Configuring Your Store" docs page

    import { applyMiddleware, compose, createStore } from 'redux'
    import thunkMiddleware from 'redux-thunk'
    
    const middlewareEnhancer = applyMiddleware(loggerMiddleware, thunkMiddleware)
    
    const composedEnhancers = compose(middlewareEnhancer, monitorReducersEnhancer)
    
    const store = createStore(rootReducer, preloadedState, composedEnhancers)
    

    【讨论】:

    • 数组的捆绑只是为了再次传播是没有必要的,直接传入参数即可。
    • 感谢您的回答,但没有奏效。经过一番尝试,我找到了解决方案,因为函数 composeWithDevTools() 是作曲家而不是增强器。
    猜你喜欢
    • 1970-01-01
    • 2018-02-19
    • 2019-10-06
    • 2014-02-14
    • 2021-10-19
    • 2020-05-01
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    相关资源
    最近更新 更多