React-Redux未捕获错误:期望reducer成为一个函数

时间:2017-05-30 18:52:32

标签: javascript reactjs redux react-redux

此项目的github回购:https://github.com/leongaban/react_starwars

预期

我的基本应用程序运行没有错误,在chrome devtools Redux选项卡中,我看到星球大战字符列表

结果

enter image description here

主要的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);

我的src&gt;还原剂&gt;字符&gt; 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&gt;字符&gt; 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&gt; index.js

import { combineReducers } from 'redux'
import characters from './characters'

export default combineReducers({
    characters
})

1 个答案:

答案 0 :(得分:0)

const createFinalStore = compose(
  applyMiddleware(thunk)
)(createStore)

const store = createFinalStore(reducer, initialState /* or {} */);

我不认为createStore redux api希望你将中间件作为第二个参数传递。应该是初始/预加载状态

http://redux.js.org/docs/api/createStore.html