如何添加“redux-thunk"和“redux-promise"? [英] How to add `redux-thunk` and `redux-promise`?

查看:69
本文介绍了如何添加“redux-thunk"和“redux-promise"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My boilerplate is reverse order from the redux-thunk docs. My boilerplate createStore is the argument, but the docs use createStore as a function. I am confuse now. How can I implement the store correctly?

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

I need to use redux-thunk. But my boilerplate is like this

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 添加到我现有的样板中?

How can I add redux-thunk to my existing boilerplate?

推荐答案

直接传给applyMiddleware

Just pass it to applyMiddleware

applyMiddleware(promise, thunk)(createStore)

或者:

createStore(reducer, applyMiddleware(promise, thunk))

这篇关于如何添加“redux-thunk"和“redux-promise"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆