2016-11-04 96 views
3

我正在製作一個React Redux示例;然而,我遇到了一個問題,得到的錯誤如下:React Redux Reducer:'this.props.tasks.map不是函數'錯誤

TypeError: this.props.tasks.map is not a function [Learn More]

我已經嘗試了很多東西,我似乎無法理解爲什麼這是行不通的。我相信這是allReducers映射Tasks功能任務的時候。我已經解決了這個錯誤,但它會抱怨它是未定義的。我會解決這個問題並回到這個問題。任何幫助,將不勝感激。我確定我犯了一個簡單的錯誤。下面是我的以下文件

App.js

import React from 'react'; 
 
import TaskBoard from "../containers/task-board"; 
 
require('../../scss/style.scss'); 
 

 
const App =() => (
 
    <div> 
 
     <h2>Task List</h2> 
 
     <hr /> 
 
     <TaskBoard/> 
 
    </div> 
 
); 
 

 
export default App;

index.js

import {combineReducers} from 'redux'; 
 
    import {Tasks} from './reducer-tasks'; 
 
    const allReducers = combineReducers({ 
 
     tasks: Tasks 
 
    }); 
 

 
    export default allReducers

任務board.js

import React, {Component} from 'react'; 
 
    import {bindActionCreators} from 'redux'; 
 
    import {connect} from 'react-redux'; 
 
    import {deleteTaskAction} from '../actions/ActionIndex'; 
 
    import {editTaskAction} from '../actions/ActionIndex'; 
 
    class TaskBoard extends Component { 
 
     renderList() { 
 
      return this.props.tasks.map((task) => { 
 
       if(task.status == "pending"){ 
 
        return (<li key={task.id}> 
 
         {task.id} {task.description} 
 
         <button type="button">Finish</button> 
 
         <button type="button">Edit</button> 
 
         <button onClick={() => this.props.deleteTask(task)} type="button">Delete</button> 
 
        </li> 
 
       ); 
 
      } 
 
     }); 
 
    } 
 
    render() { 
 
     if (!this.props.tasks) { 
 
      console.log(this.props.tasks); 
 
      return (<div>You currently have no tasks, please first create one...</div>); 
 
     } 
 
     return (
 
      <div> 
 
       {this.renderList()} 
 
      </div> 
 
     ); 
 
    } 
 
} 
 
    function mapStateToProps(state) { 
 
     return { 
 
      tasks: state.tasks 
 
     }; 
 
    } 
 
    function matchDispatchToProps(dispatch){ 
 
     return bindActionCreators(
 
     { 
 
      deleteTask: deleteTaskAction, 
 
      editTask: editTaskAction 
 
     }, dispatch) 
 
    } 
 
    export default connect(mapStateToProps,matchDispatchToProps)(TaskBoard);

減速器tasks.js

const initialState = { 
 
\t tasks: [ 
 
     { 
 
      id: 1, 
 
      description: "This is a task", 
 
      status: "pending" 
 
     }, 
 
     { 
 
      id: 2, 
 
      description: "This is another task", 
 
      status: "pending" 
 
     }, 
 
     { 
 
      id: 3, 
 
      description: "This is an easy task", 
 
      status: "pending" 
 

 
     } 
 
\t ] 
 
} 
 

 
export function Tasks (state = initialState, action) { 
 
    switch (action.type) { 
 
     case 'ADD_TASK': 
 
      return Object.assign({}, state, { 
 
      \t tasks: [ 
 
      \t \t ...state.tasks, 
 
      \t \t { 
 
      \t \t \t description: action.text, 
 
      \t \t \t status: action.status 
 
      \t \t } 
 
      \t ] 
 
      }) 
 
      break; 
 

 
     case 'EDIT_TASK': 
 
      return action.payload; 
 
      break; 
 

 
     case 'DELETE_TASK': 
 
      return Object.assign({}, state, { 
 
      \t status: action.status 
 
      }) 
 
      break; 
 
    } 
 

 
    return state; 
 
}

的actionIndex。 JS

export const addTaskAction = (task) => { 
 
     return { 
 
      type: 'ADD_TASK', 
 
      text: "Here is a sample description", 
 
      status: "pending" 
 
     } 
 
    }; 
 
    export const deleteTaskAction = (task) => { 
 
     return { 
 
      type: 'DELETE_TASK', 
 
      status: "deleted" 
 
     } 
 
    }; 
 
    export const editTaskAction = (task) => { 
 
     return { 
 
      type: 'EDIT_TASK', 
 
      payload: task 
 
     } 
 
    };

回答

3

這是因爲功能 '地圖' 只能用於數組,而不是對象。

如果你在task-board.js的渲染函數中打印出this.props.tasks,你會發現它是一個OBJECT,它包含任務數組,而不是實際任務數組本身。

因此,要解決這個問題是很容易的,而不是:

return this.props.tasks.map((task) => { 

return this.props.tasks.tasks.map((task) => { 

然後它

+0

好的,謝謝我會記住在嘗試映射之前打印該變量以查看它的對象或數組。感謝您的巧妙手段。同樣在delete_task中,我如何返回,以便將「待處理」狀態更改爲「已刪除」? – Potion

+0

至於你如何更新狀態從待處理狀態刪除? – Jayce444

+0

是的,這是正確的。 – Potion

0

根據你的減速機組成,你initialState應該是:

const initialState = [ 
    { 
     id: 1, 
     description: "This is a task", 
     status: "pending" 
    }, 
    { 
     id: 2, 
     description: "This is another task", 
     status: "pending" 
    }, 
    { 
     id: 3, 
     description: "This is an easy task", 
     status: "pending" 

    } 
] 
+0

我明白你的意思。我現在已經改變它,但我仍然得到同樣的錯誤。 TypeError:this.props.tasks.map不是函數 – Potion