2017-01-15 22 views
0

私はReduxチュートリアルに従い、簡単なTODOアプリケーションを作っています。このアプリケーションにはすでに数ページとレデューサーがあります。これは私が定義されていないので、私はすべてのtodos(私は彼らの目標と呼ぶ)を得ることができないことに注意してください。誰かがアイデアを持っていますか?Redux TODOチュートリアル、状態が定義されていません

import React from 'react'; 
import { connect } from 'react-redux'; 

import { toggleGoal } from 'containers/CounterPage/actions' 
import GoalList from 'components/GoalList' 


const getVisibleGoals = (goals, filter) => { 
    console.log(goals) 
    switch (filter) { 
    case 'SHOW_ALL': 
     return goals 
    case 'SHOW_COMPLETED': 
     return goals.filter(g => g.completed) 
    case 'SHOW_ACTIVE': 
     return goals.filter(g => !g.completed) 
    default: 
     return goals 
    } 
} 

const mapStateToProps = (state) => { 
    return { 
     goals: getVisibleGoals(state.goals, state.visibilityFilter) 
    } 
} 

const mapDispatchToProps = (dispatch) => { 
    return { 
    onGoalClick: (id) => { 
     dispatch(toggleGoal(id)) 
    } 
    } 
} 
const VisibleGoal = connect(mapStateToProps,mapDispatchToProps)(GoalList); 

export default VisibleGoal 

REDUCER

/* 
* 
* CounterPage reducer 
* 
*/ 

import { combineReducers } from 'redux'; 

const goal = (state = {} , action) => { 
    switch (action.type) { 
    case 'ADD_GOAL': 
     return { 
     id: action.id, 
     text: action.text, 
     completed: false 
     } 
    case 'TOGGLE_GOAL': 
     if (state.id !== action.id) { 
     return state 
     } 

     return Object.assign({}, state, { 
     completed: !state.completed 
     }) 

    default: 
     return state 
    } 
} 

const goals = (state = [] , action) => { 
    switch (action.type) { 
    case 'ADD_GOAL': 
     return [ 
     ...state, 
     goal(undefined, action) 
     ] 
    case 'TOGGLE_GOAL': 
     return state.map(t => 
     goal(t, action) 
    ) 
    default: 
     return state 
    } 
} 

const visibilityFilter = (state = 'SHOW_ALL', action) => { 
    switch (action.type) { 
    case 'SET_VISIBILITY_FILTER': 
     return action.filter 
    default: 
     return state 
    } 
} 

const counterPageReducer = combineReducers({ 
    goals, 
    visibilityFilter 
}) 

export default counterPageReducer; 

OUTPUT CONSOLE

Uncaught (in promise) TypeError: Cannot read property 'goals' of undefined 
    at mapStateToProps (eval at ./app/containers/VisibleGoal/index.js 

STATEツリー STATE

コンソールログを見てください。状態は機能しますが、状態を目標から外れません。 。エラーは、それが未定義

+1

減速機とコンソールログの出力を投稿してください – FabioCosta

+0

@FabioCosta更新 –

+0

正しい場所にエラーがありますか? 'state'は定義されていないと言っています(未定義のプロパティ 'ゴール'を読むことはできません) –

答えて

0

答えは目標が状態

const mapStateToProps = (state) => { 
    return { 
     goals: getVisibleGoals(state.get('counterPage').goals, state.get('counterPage').visibilityFilter) 
    } 
} 

State.get(「counterPage」)から取得した方法を変更した目標をマッピングすることはできませんと言う - >ためのcombinedReducer

関連する問題