[英]ReactJS - TypeError: this.state.items.map is not a function
I am creating my first project (To-Do List with drag and drop feature, add, delete and edit) in ReactJS as a beginner.作为初学者,我正在 ReactJS 中创建我的第一个项目(具有拖放功能的待办事项列表,添加、删除和编辑)。 I am trying to map an array but this error shows up when I am submitting a text and the code should put the item into the items array.我正在尝试映射一个数组,但是当我提交文本时会出现此错误,并且代码应该将该项目放入 items 数组中。
This will be the UI if there's no item yet in the list only a form and an input field where the user will input any string:如果列表中还没有项目,这将是 UI,只有一个表单和一个输入字段,用户将在其中输入任何字符串:
And here's the error after submitting the value:这是提交值后的错误:
Here's my current source code ( App.js
):这是我当前的源代码( App.js
):
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
items: [], //This is the items array
currentItem: {
text: "",
key: "",
},
};
this.handleInput = this.handleInput.bind(this);
this.addItem = this.addItem.bind(this);
this.deleteItem = this.deleteItem.bind(this);
this.setUpdate = this.setUpdate.bind(this);
}
handleInput(e) {
this.setState({
currentItem: {
text: e.target.value,
key: Date.now(),
},
});
}
addItem(e) {
e.preventDefault();
const newItem = this.state.currentItem;
console.log(newItem);
if (newItem !== "") {
const items = [...this.state.items, newItem];
this.setState({
items: newItem,
currentItem: {
text: "",
key: "",
},
});
}
}
deleteItem(key) {
const filteredItems = this.state.items.filter((item) => item.key !== key);
this.setState({
items: filteredItems,
});
}
setUpdate(text, key) {
console.log("items:" + this.state.items);
const items = this.state.items;
items.map((item) => {
if (item.key === key) {
console.log(item.key + " " + key);
item.text = text;
}
});
this.setState({
items: items,
});
}
render() {
return (
<div className="App">
<DragDropContext
onDragEnd={(param) => {
const srcI = param.source.index;
const desI = param.destination?.index;
if (desI) {
this.items.splice(desI, 0, this.items.splice(srcI, 1)[0]);
List.saveList(this.items);
}
}}
>
<ListContainer>
<h1>To Do List</h1>
<form id="to-do-form" onSubmit={this.addItem}>
<input
type="text"
placeholder="Enter Task"
value={this.state.currentItem.text}
onChange={this.handleInput}
/>
<button type="submit"> Add </button>
</form>
<Droppable droppableId="droppable-1">
{(provided, _) => (
<div ref={provided.innerRef} {...provided.droppableProps}>
{this.state.items.map((item, i) => (
<Draggable
key={item.id}
draggableId={"draggable-" + item.id}
index={i}
>
{(provided, snapshot) => (
<ListItem
ref={provided.innerRef}
{...provided.draggableProps}
style={{
...provided.draggableProps.style,
boxShadow: snapshot.isDragging
? "0 0 .4rem #666"
: "none",
}}
>
<DragHandle {...provided.dragHandleProps} />
<span>{item.title}</span>
</ListItem>
)}
</Draggable>
))}
{provided.placeholder}
</div>
)}
</Droppable>
</ListContainer>
</DragDropContext>
</div>
);
}
}
How to fix this problem?如何解决这个问题? I already declared items
in the top of the code inside the constructor.我已经在构造函数内的代码顶部声明了items
。 It sayd that the .map()
is for arrays only and based on the other problems I found related to this error they are using .map()
in a string instead of an array but in my problem scenario it is clearly stated that items
is already defined as an array.它说.map()
仅适用于数组,并且基于我发现的与此错误相关的其他问题,他们在字符串中使用.map()
而不是数组,但在我的问题场景中,它明确指出items
是已经定义为数组。
You have made a mistake in addItem method.您在 addItem 方法中犯了一个错误。 You need to save array of Items against items
key not the new item string.您需要根据items
键而不是新项目字符串保存项目数组。 So change that function to所以将该函数更改为
addItem(e) { e.preventDefault(); const newItem = this.state.currentItem; console.log(newItem); if (newItem !== "") { const items = [...this.state.items, newItem]; this.setState({ items: items, // or just items, currentItem: { text: "", key: "", }, }); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.