Ошибка: не удается прочитать свойство "имя" неопределенных? - PullRequest
0 голосов
/ 25 мая 2020

это приложение с файлом кода детали. 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 голосов
/ 25 мая 2020

Положите else, если значение не совпадает -

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