React / Redux:失败的prop类型-无效的prop对象

时间:2018-09-02 14:14:01

标签: json reactjs redux

我一直在努力解决这个问题,但无法解决。

提取工作正常,但在映射道具任务时收到错误消息。

JSON:

{
"tasks": [
    {
        "uuid": "d5704a95-a7f4-441f-a962-70f3962ec137",
        "title": "Finish presentation task",
        "is_completed": false,
        "created_at": "2018-09-02T14:02:04+00:00",
        "url": "http://****/todos/d5704a95-a7f4-441f-a962-70f3962ec137"
    }
]
}

现在,在提取任务后,分派任务:

export const fetchTasks = () => dispatch => {
fetch('****', {
    headers: {
        "Authorization" : `****`,
        'content-type': 'application/json'
    }
})
.then(res => res.json())
.then(tasks =>
    dispatch({
      type: FETCH_TASKS,
      payload: tasks
    })
  );
};

其次:

Tasks.propTypes = {
    fetchTasks: PropTypes.func.isRequired,
    tasks: PropTypes.array.isRequired,
    newTask: PropTypes.object
}

这返回一个错误: this.props.tasks.map不是函数

仔细观察:道具类型失败:提供给tasks的{​​{1}}类型的object无效道具Tasks。预期是array

尝试了以下更改:

tasks: PropTypes.array.isRequired
tasks: PropTypes.object.isRequired // array > object

哪个仍然返回相同的错误。

Redux devtools在状态中显示以下内容:

{
    tasks: {
         items: {
            tasks: [
                     {
                        uuid: 'd5704a95-a7f4-441f-a962-70f3962ec137',
                        title: 'Finish presentation task',
                        is_completed: false,
                        created_at: '2018-09-02T14:02:04+00:00',
                        url: 'http://****/todos/d5704a95-a7f4-441f-a962-70f3962ec137'
                     }
                   ]
                },
       item: {}
    }
}

1 个答案:

答案 0 :(得分:0)

@Arup Rakshit指出:

.then(tasks =>
    dispatch({
    type: FETCH_TASKS,
    payload: tasks.tasks
    })
);

解决了问题!