简体   繁体   中英

reducer is not a function

Try create reducers ang get data from action

But get error in console: reducer is not a function.....

My reducer:

import { INCOME_LIST } from '../actionTypes'

import Immutable from 'immutable'

    const initialUserState = {
      list: []
    }

    const listReducer = function(state = initialUserState, action) {
      switch(action.type) {
      case 'INCOME_LIST':
        return Object.assign({}, state, { list: action.data });
      }
      return state;
    }

Where I have mistake?

My Action :

import axios from 'axios'
import { INCOME_LIST } from '../actionTypes'



function receiveData(json) {
    return{
        type: INCOME_LIST,
        data: json
    }
};


export function IncomeList () {

    return dispatch => {

        return (

            axios.post('http://139.196.141.166:8084/course/income/outline',{}, {
      headers: { 'X-Authenticated-Userid': '15000500000@1' }
     }).then(function (response) {

                dispatch(receiveData(response.data));

            })

            )
    }
}

How it right way create reducer for that?

Looks like you never exported your reducer. An export default listReducer in your listReducer.js file should do the trick.

  • Store - holds our state - THERE IS ONLY ONE STATE
  • Action - State can be modified using actions - SIMPLE OBJECTS
  • Dispatcher - Action needs to be sent by someone - known as dispatching an action
  • Reducer - receives the action and modifies the state to give us a new state
    • pure functions
    • only mandatory argument is the 'type'
  • Subscriber - listens for state change to update the ui

     const initialState = { counter: 0 } const reducer = (state = initialState, action) => { switch (action.type) { case 'INCREASE_COUNTER': return { counter: state.counter + 1 } case 'DECREASE_COUNTER': return { counter: state.counter - 1 } } return state } const store = createStore(reducer) class App extends Component { render() { return ( <Provider store={store}> <CounterApp /> </Provider> ); } } 

In my case I called the function immediately

const store = createStore(rootReducer())

instead of

const store = createStore(rootReducer)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM