简体   繁体   English

反应不允许我编辑文本字段

[英]react is not letting me edit text field

Here's the code这是代码

  render () {
        if (this.props && this.props.list) {
            return (
                <div className="List_select">
                    {!this.state.editing
                    && <div><button className="List_delete" onClick={(ev) => {
                        ev.preventDefault();
                        this.props.handleDelete(this.props.list);
                    }} /> &nbsp;&nbsp;
                        <NavLink to={{
                        pathname: '/list/' + this.props.userId + '|' + this.props.list
                    }}>
                        {this.props.list}
                        </NavLink>&nbsp;&nbsp;

                        <button className="List_edit" onClick={(ev) => {
                            ev.preventDefault();
                            this.setState({editing: true})
                        }} /></div>
                    }
                    {this.state.editing
                    && <form onSubmit={(ev) => {
                        ev.preventDefault();
                        this.props.handleEdit(this.props.list, this.state.listName);
                        this.setState({editing: false})
                    }}><input type="text" name="listName" className="List_input" value={this.props.list} onChange={(ev) => {
                        this.setState({listName: ev.target.value});
                    }} />
                        <button type="submit" value="submit" name="submit" className="StandardButton-1">Submit</button>
                    </form>
                    }
                </div>
            )
        } else {
            return ""
        }
    }

The code is not letting the user fill out the text field.该代码不让用户填写文本字段。 Does anyone know what I'm doing wrong?有谁知道我做错了什么? I can edit the text fields at all.我完全可以编辑文本字段。 I have the onChange trigger and doing all the good stuff to handle text fields correct in the code.我有 onChange 触发器并做所有好事来处理代码中正确的文本字段。

You are giving it value but in onChange your changing another value.您正在赋予它价值,但在 onChange 中您正在改变另一个价值。 Have the same value in both :)在两者中具有相同的值:)

   <input type="text" name="listName" className="List_input" 
     value={this.state.listName} 
     onChange={(ev) => {this.setState({listName: ev.target.value})  }}                       
   />
value={this.props.list} onChange={(ev) => {
                    this.setState({listName: ev.target.value});

the listName and list must match, and you have to use this.state.list listName 和 list 必须匹配,你必须使用 this.state.list

You have to assign the text box value from state not props , because props is readonly.您必须从state而不是props分配文本框值,因为props是只读的。 So you have to replace this.props.list with this.state.listName所以,你必须更换this.props.listthis.state.listName

<input
  type="text"
  name="listName"
  className="List_input"
  value={this.state.listName}
  onChange={ev => {
    this.setState({ listName: ev.target.value });
  }}
/>

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

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