【问题标题】:Use Custom Async middleware to dispatch - Redux Thunk - react Router使用自定义异步中间件进行调度 - Redux Thunk - react Router
【发布时间】:2018-06-12 06:15:32
【问题描述】:

我正在尝试使用 redux thunk 分发异步操作。 但我的代码不起作用,控制台只是向我抛出错误 Error: Actions must be plain objects. Use custom middleware for async actions.

好吧,这是我的代码:

    import React from 'react';
import ReactDOM from 'react-dom';
import 'babel-polyfill';
import ReduxThunk from 'redux-thunk';
import { createStore, combineReducers,applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import { Router, Route, IndexRoute, browserHistory } from 'react-router';
import { syncHistoryWithStore, routerReducer } from 'react-router-redux';
import App, { reducer } from './AppContainer';
import {launchReducer} from './modules/Financeiro/Lancamentos/reducer/launchReducer';
import {Rotas} from './routes';

// Add the reducer to your store on the `routing` key
const store = createStore(
    combineReducers({
        app: reducer,
        routing: routerReducer,
        launch: launchReducer
    }),
    applyMiddleware(ReduxThunk),
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(),
);

// Create an enhanced history that syncs navigation events with the store
const history = syncHistoryWithStore(browserHistory, store);

document.getElementById('app').style['min-height'] = '100vh';

ReactDOM.render(
    <Provider store={store}>
        <Router history={history}>
            {Rotas.map(rota => (
                <Route path={rota.path} component={App}>
                    <IndexRoute component={rota.component} />
                </Route>)
            )}
        </Router>
    </Provider>,
    document.getElementById('app') // eslint-disable-line comma-dangle
);

而我的动作代码是这样的:

  export const newLaunch = (obj) => async (dispatch) => {
    delete obj.idLaunch_headerOrigin
    let resp = await APIService('http://localhost:3000/launch/newLaunch', obj, 'POST')
    dispatch({type: SET_LAUNCH_HEADER_ORIGIN, payload: resp.response})
  }

在组件中,我只是导入动作并从 react-redux 放入连接,我用“this.props.newLaunch(obj)”调用它

编辑: 我的错误出现在 createStore 中,create store 接收到 3 个参数,我在将 prelaoders 放置在 enchances 位置时出错了,我只是将骑手从第 2 个参数换到第 3 个参数就完成了,我的最终代码是:const store = createStore( combineReducers({ app: reducer, routing: routerReducer, launch: launchReducer }), window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(), applyMiddleware(ReduxThunk), );

感谢您的帮助,英语不好请见谅!

【问题讨论】:

  • 您必须为您的操作添加 Promise 支持,因为您的 API 返回一个承诺。 redux-promise 应该可以帮到你。
  • 但是 redux-thunk 提供这个问题不是吗?或者 redux-promise 有另一个功能。在我所有的项目中,我只使用了 redux-thunk,这就是我调度异步操作所需要的全部
  • 我知道redux的promise是返回一个promise,dispatch返回一个函数!但在我的操作中,我使用 async/await 来等待响应并从 API 的响应中调度某个属性。

标签: reactjs react-router react-redux redux-thunk react-router-redux


【解决方案1】:

您以错误的方式配置商店。 createStore 接收 3 个参数(reducer、preloadedState、enhancer)。因为您在代码中发送 3,所以将“applyMiddleware(ReduxThunk)”作为预加载状态。

查看此链接以了解在使用 redux-thunk 等增强器时如何配置 redux-devtools:https://github.com/zalmoxisus/redux-devtools-extension#12-advanced-store-setup

【讨论】:

  • 天哪!多谢!我什至没有注意到!感谢您阅读我的代码和准确的答案
猜你喜欢
  • 2016-12-20
  • 1970-01-01
  • 2020-05-16
  • 2018-09-10
  • 2021-08-24
  • 2018-01-10
  • 2020-09-21
  • 1970-01-01
  • 2019-11-12
相关资源
最近更新 更多