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

IT技术 javascript reactjs
2021-05-07 04:09:12

每次尝试在输入字段中输入内容时,我都无法在代码中找到解决此错误的方法。类型错误:无法读取未定义的属性“名称”

我是新来的react,不太了解。

这是我文件中的所有代码

import React from'react';
import Notelist from '../componenets/notelist';
import { Link } from 'react-router-dom';

export default class NewPage extends React.Component {
    state = {
        note:{
            title: '',
            body: '',
            createdAt: undefined,
            updatedAt: undefined

        }
    }

    updateValue = (e) => {
        const { note } = this.state;

        this.setState({
            note: { ...note, [e.title.name]: e.target.value }
        });
    }

    handleSave = (e) => {
        e.preventDefault();

        const id = this.props.onSave(this.state.note);

        this.props.history.replace(`/notes/${ id }`);
    }

    render() {
        const { note } = this.state;

        return (
            <div className="note-form">
                <h1>New Note</h1>
                <form onSubmit={this.handleSave}>
                    <div className="note-form-field">
                        <label>Title: </label><br/>
                        <input className="input-txt" type="text" name="title" value={note.title} onChange={this.updateValue}/>
                    </div>
                    <div className="note-form-field note-form-field-text">
                        <br/>
                        <textarea name="body" value={note.body} onChange={this.updateValue} />
                    </div>
                    <div className="note-form-buttons">
                        <button className="btn">Save</button>
                        <Link to="/">Cancel</Link>
                    </div>
                </form>
            </div>
        );
    }
}

这是我得到的完整错误:

类型错误:无法读取未定义的 NewPage.updateValue C:/Users/user/react-notes/src/pages/new.js:20 的属性“名称”

  17 |        const { note } = this.state;
  18 | 
  19 |        this.setState({
> 20 |            note: { ...note, [e.title.name]: e.target.value }
     | ^  21 |        });
  22 |    }
  23 | 

查看编译

4个回答

event物体(五)没有一个叫关键title这就是为什么你会得到“无法读取的未定义的属性名称”。

您需要event.target,它指的是导致此事件发生的元素。

尝试以下动态更新state-value. 它将找到一个与name元素的匹配的键,并为其提供该元素的值(如用户输入):

updateValue = (e) => {
    const { note } = this.state;

    this.setState({
        note: { ...note, [e.target.name]: e.target.value }
    });
}

我相信您正在尝试将title输入字段的键和值分配为值。你为什么不试试这个?

updateValue = (e) => {
        const { note } = this.state;

        this.setState({
            note: { ...note, "title": e.target.value }
        });
    }

updateValue 方法中的参数 e 是事件对象,它不会有名为 title.name 的属性。如果你想把title文本框的值存储到note对象的属性title中,那么你的代码可以是这样的

this.setState({
      note: { ...note, title: e.target.value }
    });

导入 React, { Component } from 'react'

导出类 Todolist2 扩展组件 {

state={name:'',items:'',price:'',arr:[],errors:{name:'',items:'',price:''}}

todoSubmit=(event)=>
{
    event.preventDefault()
  let list=this.state.arr;
  let tododata={name:this.state.name, items:this.state.items, price:this.state.price};
  list.push(tododata);
  this.setState({arr:list});
  console.log(this.state.arr);

 

}


handle=(event)=>
{
    const {name,value}=event.target;
    let errors=this.state.error;
    switch(errors)
    {
        case 'name':errors.name=value.length<2?"Name must be 2 character long":'';

        case 'items':errors.items=value.length<2?"items must be 2 character long":'';

        case 'price':errors.price=value.length<2?"price must be 2 character long":'';
    }
    this.setState({errors,[name]:value})

    this.setState({[name]:value})
    console.log(this.state);
}


deldata=(ind)=>
{
    if (window.confirm('Do u want to delete'))
    {
    let list=this.state.arr;
    list.splice(ind,1);
    this.setState({arr:list});
    }
    // alert('do u want to delete');
}

render() {

    const {errors}=this.state;
    return (
        <div>
            <h1>ToDo List 2</h1>
            <form className='container' onSubmit={this.todoSubmit}>
                <div className="form-group col-md-4">
                    <label>Name:</label>
                    <input type="text" name="name"  className='form-control' onChange={this.handle}/>
                    {errors.name.length>0 && <span className='alert alert-danger'>{errors.name}</span> }
                    
                </div>
                <div className="form-group col-md-4" onChange={this.handle}>
                    <label>Items:</label>
                    <input type="text" name="items"  class='form-control'/>
                    {errors.items.length>0 && <span className='alert alert-danger'>{errors.items}</span> }
                </div>
                <div className="form-group col-md-4" onChange={this.handle}>
                    <label>Price:</label>
                    <input type="text" name="price"  class='form-control'/>
                    {errors.price.length>0 && <span className='alert alert-danger'>{errors.price}</span> }
                </div>
                <input type='Submit' value='Submit' class='btn btn-outline-success'  />
               

                <table class='table'>
                    <tr>
                        <th>S.no</th>
                        <th>Name</th>
                        <th>Items</th>
                        <th>Price</th>
                        <th>Action</th>
                    </tr>
                    {this.state.arr.map((data,ind)=>
                    
                       <tr>
                           <td>{ind+1}</td>
                           <td>{data.name}</td>
                           <td>{data.items}</td>
                           <td>{data.price}</td>
                           <td><button  class="btn btn-info btn-lg " onClick={()=>this.deldata(ind)}>delete </button></td>
                       </tr>
                    )}
                </table>

            </form>
        </div>
    )
}

}

导出默认 Todolist2