我正在製作一個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
}
};
好的,謝謝我會記住在嘗試映射之前打印該變量以查看它的對象或數組。感謝您的巧妙手段。同樣在delete_task中,我如何返回,以便將「待處理」狀態更改爲「已刪除」? – Potion
至於你如何更新狀態從待處理狀態刪除? – Jayce444
是的,這是正確的。 – Potion