reactjs 如何使用www.example.com呈现列表项this.state.items.map?

xvw2m8pv  于 2023-01-02  发布在  React
关注(0)|答案(3)|浏览(109)

所以我正在学习用rails API构建react todo列表的教程。
当我尝试将任务添加到列表时,它会显示项目符号而不是文本。检查控制台时,它告诉我错误是:

"Warning: Each child in a list should have a unique "key" prop.

但我很确定我已经分配了一个关键 prop ?
我检查了有问题的代码行{this.state.items.map((item) => (<TodoItem key={item.id} item={item} >{item}</TodoItem>,它看起来是正确的。所以我不确定我在这里做错了什么?这是我的TodoList. js

import React, { Component } from "react";
import TodoForm from "./TodoForm";
import TodoItem from "./TodoItem";

const  api_url = "http://localhost:3001/api/v1/todos"

class TodoList extends Component {
    constructor(props) {
      super(props)
      this.state = {
        items: []
      }
      this.updateTodoList = this.updateTodoList.bind(this);

    }

    componentDidMount () {
      this.getTasks();
    }

    getTasks() {
      fetch(api_url)
        .then(response => response.json())
        .then(response_items => {
          this.setState({
            items: response_items.reverse()
          })
      });
    }

    updateTodoList(item) {
      let _items = this.state.items
      // unshift adds to beginning of array
      _items.unshift(item)
      this.setState({
        items: _items
      })
    }

    render() {
        console.log(this.state.items)
        return (
            <div>
                <TodoForm api_url={api_url} updateTodoList={this.updateTodoList} />
                <ul id="todo_list">
                    {this.state.items.map((item) => (
                        <TodoItem key={item.id} item={item} >{item}</TodoItem>
                    ))}
                </ul>
            </div>
        )
    }
}
export default TodoList;

这是我的ToDoItem. js

import React from "react";

export default function TodoItem(props) {
    return (
        <li>{props.item.task}</li>
    )
}

任何帮助了解和修复此错误将不胜感激。

rn0zuynd

rn0zuynd1#

您的item.id不唯一。请尝试console.log您的item.id以检查它们是否唯一。
或者尝试:

{this.state.items.map((item, index) => (
    <TodoItem key={index} item={item}>{item}</TodoItem>
))}
5vf7fwbs

5vf7fwbs2#

我修复了这个错误。我在FormSubmit函数中的'response.json'后面缺少一个括号。

async formSubmit(formData) {
      var data = new FormData(formData)
      await fetch(this.state.api_url, {
        method: "POST",
        mode: "cors",
        body: data
      }).then(response => response.json)
      .then(response => this.props.updateTodoList(response))
    }

变成

async formSubmit(formData) {
      var data = new FormData(formData)
      await fetch(this.state.api_url, {
        method: "POST",
        mode: "cors",
        body: data
      }).then(response => response.json())
      .then(response => this.props.updateTodoList(response))
    }

这就解决了问题。

quhf5bfb

quhf5bfb3#

    • React建议我们不要使用索引作为键,而是使用某种唯一标识字符串。**

例如:你可以使用像'uuid'这样的包,它基本上只允许你通过把它作为一个包安装来生成uuid()。
步骤1-npm安装uuidv4
步骤2-从"uuid"导入{v4作为uuidv4};
ste3-编辑您代码行,如下所示

{this.state.items.map((item) => (<TodoItem key={uuidv4()} item={item} >{item}</TodoItem>

Note : items.map((item, index) => key={index}), This is going to be index of current todo item that's being looped through the items array.
UUID:通用唯一标识符(UUID)是许多非多值数据库和软件中使用的标识符标准,用于在不使用增量数字的情况下生成唯一ID。

相关问题