【问题标题】:Why react default form feature is not executing?为什么反应默认表单功能没有执行?
【发布时间】:2022-07-11 22:40:30
【问题描述】:
import React from "react";

export default class Form extends React.Component{
    constructor(props) {
        super(props);
        this.inputRef = React.createRef();
        this.state = {
            value:""
        }
    }

    // handleClick
    handleClick = (e) => {
        this.setState({ value: e.target.value })
        console.log(e.target.value)
    }

    render() {
        return <>
            <h2>Typig ... {this.state.value}  </h2>
            <form>
                <input type="text" ref={this.inputRef} onChange={this.handleClick}  />
            </form>
        </>
    }
}

我了解到我们不能在 react 中更改任何输入标签的值,我们必须通过编写处理函数手动完成,但是在上面的代码 sn-p 中我没有显式更改值那么为什么这里是默认值未应用行为 在上面的代码sn-p中我没有明确地改变输入标签的值那么为什么这里没有应用react默认特性

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    像这样转换你的输入:

     <input type="text" value={this.state.value} ref={this.inputRef} onChange={e => this.handleClick}  />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-17
      • 2020-12-14
      • 2022-12-19
      • 2013-09-08
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多