【问题标题】:How to add `redux-thunk` and `redux-promise`?如何添加 `redux-thunk` 和 `redux-promise`?
【发布时间】:2017-12-12 07:22:53
【问题描述】:

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

【问题讨论】:

  • 我的想法是从项目中删除redux-promise。但我会避免它。因为我不知道我会需要什么样的情况。

标签: redux redux-thunk


【解决方案1】:

只需将其传递给 applyMiddleware

applyMiddleware(promise, thunk)(createStore)

或者:

createStore(reducer, applyMiddleware(promise, thunk))

【讨论】:

  • 你怎么知道createStoreapplyMiddleware的使用模式?
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2016-08-03
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    • 2018-05-27
    • 2018-11-10
    • 2020-07-12
    • 1970-01-01
    • 2019-03-23
    相关资源
    最近更新 更多