当我编写无法访问商店的自定义中间件时,似乎出现了问题。还有其他人遇到过这个问题吗?
// custom middleware
const API = ({ getState, dispatch }) => next => action => {
console.log(getState); // undefined
console.log(dispatch); // undefined
fetch('http://localhost:3000/')
.then(response => {
const { ok, status } = rawRes;
})
.catch(err => console.error('There appears to be an error in the API Client.', err));
return next(action);
};
export default API;
// creating the store
import { createStore, applyMiddleware } from 'redux';
import logger from 'redux-logger';
import API from './middlewares/API';
import Reducer from './reducers/index';
const Store = createStore(Reducer, applyMiddleware(
logger,
API
));
export default Store;
我还将商店传递给我的提供者,然后我“连接”我的一个子组件,因此实际上可以调度一个动作。但是,当它到达我的中间件时,商店就丢失了,但是动作和下一个似乎就在那里了。我不知道为什么。任何帮助,将不胜感激。