2017-08-08 62 views
-2
import {FETCH_OBSERVATION} from '../actions/index'; 
import {DELETE_OBSERVATION} from '../actions/index'; 

export default function(state={all:[],count:null},action){ 
    switch (action.type) { 
    case FETCH_OBSERVATION: 
    console.log("data for fetch observationCount",action.payload.data.model.observationInstanceList); 

    return{ 
     all:state.all.concat(action.payload.data.model.observationInstanceList), 
     count:action.payload.data.model.observationCount 
    } 
    case DELETE_OBSERVATION: 
    return state.all=[]; 
    default: 
    return state 

    } 
    return state; 
} 

我使用行動/索引文件中的動作創建者定義從API中提取數據。 有人可以告訴我爲什麼state.all.concat沒有定義?想要返回不同的值從相同reducer在reactjs

錯誤是這個樣子

Cannot read property 'concat' of undefined 
at ./src/reducers/reducer_observation.js.__webpack_exports__.a (reducer_observation.js:10) 
at combination (combineReducers.js:120) 
at dispatch (createStore.js:165) 
at redux-logger.js:1 
at index.js:28 
at dispatch (applyMiddleware.js:35) 
at action.payload.then._extends.payload (index.js:25) 
at <anonymous> 

我結合減速這個樣子

import { combineReducers } from 'redux'; 
import SpeciesChartReducer from './reducer_chart_data'; 
import ObservationReducer from './reducer_observation'; 
import TaxonReducer from './reducer_taxon_list'; 
import UserGroupNames from './reducer_fetch_userGroup'; 
import UserGroupObservations from './reducer_fetch_groupobservations'; 
import HomeTotalCount from './reducer_home_total_count'; 

const rootReducer = combineReducers({ 
    Observation:ObservationReducer, 
    ChartData:SpeciesChartReducer, 
    treeData:TaxonReducer, 
    UserGroupNames:UserGroupNames, 
    UserGroupObservations:UserGroupObservations, 
    HomeTotalCount:HomeTotalCount 
}); 

export default rootReducer; 
+0

你到底用'的console.log thing'是什麼意思?這個:'action.payload.data.model.observationInstanceList'? – Denialos

+0

用'return state.all = [];'你正在改變狀態並返回一個不同形狀的狀態。我很確定那不是你想要做的。您可能想要再次返回默認狀態。最好把它解壓到'const'。例如:'const DEFAULT_STATE = {all:[],count:null};'在reducer外部,內部:'function(state = DEFAULT_STATE,action){'及以後:case DELETE_OBSERVATION:return DEFAULT_STATE;'。 – Yoshi

+0

其實我在觀察頁面上實現過濾器。因此,一旦新的過濾器應用我想刪除顯示的觀察,並加載新的觀察,這就是爲什麼我做這個「state.all:[]」後刪除前一個。 如果你有任何想法,請分享考慮上述情況。 我正在開發的項目是OpenSource,你可以在這裏看看功能網站。 http://indiabiodiversity.org/observation/list?view=list 上面的頁面是我想要實現的。 – user2903536

回答

0

好了,所以如果u希望上面的函數的工作,然後做這樣的。

import {FETCH_OBSERVATION} from '../actions/index'; 
import {DELETE_OBSERVATION} from '../actions/index'; 
const DEFAULT_STATE={all:[],count:null} 
export default function(state=DEFAULT_STATE,action){ 
    switch (action.type) { 
    case FETCH_OBSERVATION: 
    return{ 
     all:state.all.concat(action.payload.data.model.observationInstanceList), 
     count:action.payload.data.model.observationCount 
    } 
    case DELETE_OBSERVATION: 
    return DEFAULT_STATE; 
    default: 
    return DEFAULT_STATE 

    } 
    return DEFAULT_STATE; 
} 

`

相關問題