如何添加`redux-thunk`和`redux-promise`?

时间:2017-12-12 07:22:54

标签: redux redux-thunk

我的样板文件与redux-thunk文档的顺序相反。 我的样板createStore是参数,但文档使用createStore作为函数。我现在很困惑。如何正确实施store

我需要使用redux-thunk。但我的样板是这样的

import React, {Component} from 'react';
import './App.css';
import SelectTeam from "./components/select_teams";
import reducers from './reducers/index';
import {Provider} from 'react-redux';
import promise from "redux-promise";
import {applyMiddleware, createStore} from 'redux';
import {BrowserRouter, Route, Switch, Redirect} from 'react-router-dom';
import LoginPage from './components/loginPage';

const createStoreWithMiddleware = applyMiddleware(promise)(createStore);
...
render() {
    return (
      <Provider store={createStoreWithMiddleware(reducers)}>
        <BrowserRouter>
       ....

这是官方文档

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers';

// Note: this API requires redux@>=3.1.0
const store = createStore(
  rootReducer,
  applyMiddleware(thunk)
);

如何将redux-thunk添加到我现有的样板?

2 个答案:

答案 0 :(得分:3)

将它传递给applyMiddleware

applyMiddleware(promise, thunk)(createStore)

或者:

createStore(reducer, applyMiddleware(promise, thunk))

答案 1 :(得分:0)

我用这个。但我不确定它是否正确,但至少是这样。它不会破坏我的应用程序(仅限现在)。

import thunk from 'redux-thunk';
...
<Provider store={createStoreWithMiddleware(reducers, applyMiddleware(thunk))}>