得到错误“期望减速器成为一种功能。”?

时间:2017-07-21 02:34:43

标签: reactjs redux react-router react-redux

我收到错误“预计减速器是一个功能。”

我正在尝试在我的项目中添加redux。我创建reducer但也会收到错误“预期reducer将成为一个函数。”我的代码是

https://codesandbox.io/s/k5Gp2gglx

import React from 'react';
import { render } from 'react-dom';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import {Provider} from 'react-redux';
import {combindReducer} from './reducers/index'

import Hello from './Hello';

const styles = {
  fontFamily: 'sans-serif',
  textAlign: 'center',
};

const App = () => (
  <div style={styles}>
    <Hello name="CodeSandbox" />
    <h2>Start editing to see some magic happen {'\u2728'}</h2>
  </div>
);
const store = createStore(combindReducer,
                         applyMiddleware(thunk)
                         );
render(
  <Provider store ={store}>
    <App />
    </Provider>
  , document.getElementById('root'));

合并缩减器

import {combineReducers } from 'redux';
import {first_redux} from './topstories';

const combindReducer = combineReducers({
    item: first_redux
})

export default combindReducer;

topstories

export default function(state = [], action) {
    switch (action.type) {
        case 'ADD_ITEM':
            return [
                ...state,
                action.payload
            ];
        case 'DELETE_ITEM':
            const index = state.indexOf(action.payload);
            if (index === -1) {
                return state;
            }
            return state.slice(0, index).concat(state.slice(index + 1));
        case 'UPDATE_ITEM':
            let newList = state.slice()
            newList.splice(action.payload.index, 1, action.payload.item)
            return newList;
        default:
            return state

    }
}

1 个答案:

答案 0 :(得分:2)

由于您使用的是默认导出(不是名为exports),因此reducers的import语句不应该有大括号:

import combindReducer from './reducers/index';

import first_redux from './topstories';

https://codesandbox.io/s/L8RE3X5D4