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