错误:无法读取未定义的属性“名称”?

时间:2020-05-25 14:05:46

标签: javascript reactjs setstate

这是零件代码文件app.js

....
this.state = {
      todoList: [
        { id: 1, name: "Khoa" },
        { id: 2, name: "Linh" },
        { id: 3, name: "Luc" },
      ],
      inputText: "",
      currentName: "",
      todoSearch : []
    };
  }
....
handleSubmit = (e, inputRef) => {
    const { todoList, currentName } = this.state;
    if (inputRef.current.value.trim() === "")
      alert("Hay nhap du lieu can input");
    else {
      if (currentName !== "") {
         this.setState({
           todoList : todoList.map(item => {
             if(item.name === currentName) {
               return {...item, name : inputRef.current.value}// error this code
             }
           }),
           currentName : ''
         })
      } else {
        const data = { id: todoList.length + 1, name: inputRef.current.value };
        console.log("say hi");
        this.setState({
          todoList: [...todoList, data],
          currentName : ''
        });
      }
    }
  };
.....

我想要更新数据不令人满意item.name === currentName。含义替换todoList中对象的旧名称等于新名称为inputRef.current.value,但不起作用。帮帮我

1 个答案:

答案 0 :(得分:0)

如果该值不匹配,则输入其他值-

todoList : todoList.map(item => {
         if(item.name === currentName) {
           return {...item, name : inputRef.current.value}// error this code
         }else{
           return {...item}
       })