react生成的表单不能键入

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了react生成的表单不能键入相关的知识,希望对你有一定的参考价值。

刚学react,用react做了一个表单,生成后却发现,input输入不了内容,经百度后,发现react生成的表单如果要重新渲染其中的value应该改变state才会重新渲染,所以要给input绑定onChange事件,具体实现如下所示:

FormItem.js

class FormItem extends React.Component {
    constructor(props) {
        super(props);
        this.onInputChange = this.onInputChange.bind(this);
        this.state = { value: this.props.itemInfo.input.value };
    }

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

    render() {
        let {  label, id, input } = this.props.itemInfo,
            formId = this.props.formId || null,
            help = input.help ? <p className="addAeraHelp">{input.help}</p> : null;
        input.type = input.type || ‘text‘;
        input.placeholder = input.placeholder || null;
        return (
            <div className="form-group" id={formId}>
                <label for={formId} className="col-sm-2 control-label">{label}</label>
                <div className="col-sm-10">
                    <input type={input.type} className="form-control" id={formId + id} name={input.name || null} placeholder={input.placeholder} value={this.state.value} readOnly={input.readOnly} onChange={this.onInputChange} required />
                </div>
                {help}
            </div>
        );
    }
}

 

以上是关于react生成的表单不能键入的主要内容,如果未能解决你的问题,请参考以下文章

前端开发工具vscode如何快速生成代码片段

如何使用 onSubmit 键入表单组件?

React Antd 关于Input表单清空的坑

React-native hooks 表单验证

react.js 基于 DRF HTTP OPTIONS 动态生成表单

是否有更干的方法来创建React文本输入表单元素?