[英]React-Redux Uncaught Error: Expected the reducer to be a function
The github repo for this project: https://github.com/leongaban/react_starwars 该项目的github仓库: https : //github.com/leongaban/react_starwars
My basic app runs without errors, and in the chrome devtools Redux tab, I see a list of star wars characters 我的基本应用程序运行无误,在chrome devtools的Redux标签中,我看到了星球大战角色列表
The main index.js 主要的index.js
import React from 'react'
import ReactDOM from 'react-dom'
import App from './components/App'
import { createStore, applyMiddleware, compose } from 'redux'
import { Provider } from 'react-redux'
import thunk from 'redux-thunk'
import reducer from './reducer'
import { getCharacters } from './reducer/characters/actions'
const store = createStore(reducer, compose(
applyMiddleware(thunk),
window.devToolsExtension ? window.devToolsExtension : f => f
));
store.dispatch(getCharacters())
const container = document.getElementById('app-container');
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>
, container);
My src > reducer > characters > actions.js 我的src>减速器>字符> actions.js
import { API_URL } from '../../constants'
export const SET_CHARACTERS = 'SET_CHARACTERS'
export function getCharacters() {
return dispatch => {
fetch(`${API_URL}/people`)
.then(res => res.json())
.then(res => res.results)
.then(characters =>
dispatch(setCharacters(characters))
)
}
}
export function setCharacters(characters) {
return {
type: SET_CHARACTERS,
characters
}
}
reducer > characters > index.js 减速器>字符> index.js
import { SET_CHARACTERS } from './actions'
const initialState = [];
export default (state = initialState, action) => {
switch(action.type) {
case SET_CHARACTERS:
return action.characters;
default:
return state;
}
}
reducer > index.js 减速器> index.js
import { combineReducers } from 'redux'
import characters from './characters'
export default combineReducers({
characters
})
try 尝试
const createFinalStore = compose(
applyMiddleware(thunk)
)(createStore)
const store = createFinalStore(reducer, initialState /* or {} */);
i don't think the createStore redux api wants you to pass the middleware as the second argument. 我不认为createStore redux api希望您将中间件作为第二个参数传递。 that should be the initial/preloaded state 那应该是初始/预加载状态
http://redux.js.org/docs/api/createStore.html http://redux.js.org/docs/api/createStore.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.