简体   繁体   English

React Material UI更新TextField值

[英]React Material UI Updating TextField Values

I start the session like this 我像这样开始会议

state = {
    firm: null,
    office: null,
    salesCode: null,
    account: null
}

I have each TextField showing like this: 我有每个TextField显示如下:

<TableCell>
    <TextField
        id="standard-bare"
        defaultValue={items.data[i].firm}
        margin="normal"
        onChange={(e) => this.handleChange(e, items.data[i].id)}
    />
</TableCell>

Then I have a handleChange event 然后我有一个handleChange事件

handleChange({ event, id }) {
    const { data } = this.state;
    data[id] = event.target.value;
    this.setState({data})
}

When I run and edit the text inside TextField I get the below error 当我运行并编辑TextField中的文本时,出现以下错误

TypeError: Cannot read property 'target' of undefined
UserDataTable.handleChange:75
  72 |    
  73 |    handleChange({ event, id }) {
  74 |        const { data } = this.state;
> 75 |        data[id] = event.target.value;
     | ^  76 |        this.setState({data})
  77 |    }
  78 | 

I am trying to implement edit function to the table. 我正在尝试对表实施编辑功能。 So if user edits the text, I get the new updated information and do the API call to update the values accordingly there. 因此,如果用户编辑文本,我将获得新的更新信息,并进行API调用以在那里相应地更新值。

You are not passing an object to handleChange to extract but more of individual params so 您没有将对象传递给handleChange来提取,而是传递了更多的单个参数,因此

Change 更改

handleChange({ event, id }) {

To

 handleChange(event, id) {

Edit: 编辑:

There is no data defined in state but you are accessing it and trying to set input value to the data object by id which isn't correct. 状态中未定义任何数据,但您正在访问它,并尝试通过ID设置输入值给数据对象,这是不正确的。 It should be something like below 它应该像下面这样

     handleChange(event, id) {
        this.setState({
              [id]: event.target.value
        })
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM