React Redux Reducer:'this.props.tasks.map不是函数'错误

问题描述:

我正在制作一个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 
 
     } 
 
    };

这是因为功能 '地图' 只能用于数组,而不是对象。

如果你在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

根据你的减速机组成,你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