简体   繁体   English

更新组件时,React输入元素未更新

[英]React Input Element Not Updating When Updating Component

I'm trying to make a React version of this . 我试图做的版本做出反应这个

You can see what I have so far on this Codepen . 您可以看到到目前为止我对Codepen的了解

The issue here is that when you type into a text field (under the "Suspected Player" column) and then click the adjacent name (under the "Role" column) to remove that row, the text persists inside the input, and is then adjacent to a different name. 这里的问题是,当您在文本字段中(在“怀疑的播放器”列下)键入内容,然后单击相邻的名称(在“角色”列下)以删除该行时,文本将保留在输入中,然后与其他名称相邻。
The expected behavior is that you'd click the name to remove the row and both the elements (the name and text input) would disappear. 预期的行为是,您将单击名称以删除该行,并且两个元素(名称和文本输入)都将消失。

All the items in the table are stored in an array called checklist in the state of the RoleList class. 表中的所有项目都以RoleList类的状态存储在称为checklist的数组中。

The component for each list item in the table is this: 表中每个列表项的组成部分是这样的:

class RoleCheckpoint extends React.Component {
    constructor() {
        super();
        this.state = {
            text: ""
        };
    }

    deleteThis() {
        this.props.removeRole(this.props.role.id);
    }

    onChange(e) {
        this.setState({
            text: e.target.value
        });
    }

    render() {
        console.log(this.props);

        return (
            <tr>
                <td className="vertAlign remove noselect" onClick={this.deleteThis.bind(this)}>
                    {this.props.role.el}
                </td>
                <td>
                    <input type="text" className="form-control" spellCheck="false" onChange={this.onChange.bind(this)} value={this.state.text} />
                </td>
            </tr>
        );
    }
}

When you click on the name it invokes the components deleteThis function, which in turn invokes RoleList 's removeRole function (that goes through the array of list items, and removes the one with the matching ID from the components state), which is being passed as a prop to RoleCheckpoint . 当您单击名称时,它将调用组件deleteThis函数,该函数依次调用RoleListremoveRole函数(遍历列表项的数组,并从组件状态中删除具有匹配ID的那个),该函数将被传递作为RoleCheckpoint的道具。

I've tried using this.forceUpdate() in the deleteThis function, and as you can see I've tried binding the input field's value to the components state (called text ), yet still the input doesn't update when removing the component from the DOM. 我已经尝试在deleteThis函数中使用this.forceUpdate() ,并且如您所见,我已经尝试将输入字段的value绑定到组件状态(称为text ),但是在删除组件时输入不会更新来自DOM。

Any help is appreciated, if you need more clarification I'll be happy to provide it. 感谢您的帮助,如果您需要更多说明,我们将很乐意为您提供帮助。

“键”是创建元素列表时需要包括的特殊字符串属性。

<RoleCheckpoint role={role} key={role.id} removeRole={this.removeRole.bind(this)} />

The behaviour is typical when you have a list of components and you have not set the key of the objects correctly. 当您具有组件列表并且未正确设置对象的键时,此行为是典型的。 It need to be something that identifies the object. 它必须是识别对象的东西。 Probably you can use the name field: 可能您可以使用名称字段:

<RoleCheckpoint key={role.name} .../>

See https://facebook.github.io/react/docs/lists-and-keys.html 参见https://facebook.github.io/react/docs/lists-and-keys.html

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

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