Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 未捕获类型错误:无法创建属性';empFirstName';关于字符串'';_Reactjs - Fatal编程技术网

Reactjs 未捕获类型错误:无法创建属性';empFirstName';关于字符串'';

Reactjs 未捕获类型错误:无法创建属性';empFirstName';关于字符串'';,reactjs,Reactjs,我在我的ReactJS应用程序中遇到了这样的错误 未捕获的TypeError:无法在字符串“”上创建属性“empFirstName” 在我使用“清除”按钮清除文本框并尝试键入文本后,浏览器控制台中会显示此错误 代码如下: import React from 'react'; import fetch from 'isomorphic-fetch'; import 'bootstrap/dist/css/bootstrap.css'; import 'bootstrap/dist/css/boot

我在我的ReactJS应用程序中遇到了这样的错误

未捕获的TypeError:无法在字符串“”上创建属性“empFirstName”

在我使用“清除”按钮清除文本框并尝试键入文本后,浏览器控制台中会显示此错误

代码如下:

import React from 'react';
import fetch from 'isomorphic-fetch';
import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap/dist/css/bootstrap-theme.css';
import FormErrors from './FormErrors.js';

export default class EmployeeRegister extends React.Component {

    constructor() {
        super();
        this.state = {
            empFirstName: ''     
        }
        this.handleChange = this.handleChange.bind(this);
        this.clearFields = this.clearFields.bind(this);
    }

    handleChange(event) {
        const target = event.target;
        const value = target.value;
        const name = target.name;
        this.setState({[name]: value})
        });
    }

clearFields() {

        this.setState({empFirstName: ''})

    }

 render() {

        return (

            <div>

                <div className="row">
                    <div className="col-sm-12">
                        <h2>Employee Register Form</h2>
                    </div>
                </div>



                <form>

                    <div className="row">
                        <div className="col-sm-6">

                            <div className="form-group">
                                <label htmlFor="empFirstName">First Name : </label>
                                <input type="text" className="form-control" id="empFirstNameTxt" value={this.state.empFirstName}
                                       name="empFirstName"
                                       onChange={this.handleChange}/>
                            </div>

                        </div>

                    </div>



                    <div className="row">

                    <input type="submit" value="Submit" className="btn btn-primary" id="submitBtn" />
                             <input type="button" value="Clear"
                           className="btn btn-warning"
                           onClick={this.clearFields}/>


                    </div>


                </form>

            </div>



        );
    }
};
从“React”导入React;
从“同构提取”导入提取;
导入'bootstrap/dist/css/bootstrap.css';
导入'bootstrap/dist/css/bootstrap theme.css';
从“/FormErrors.js”导入FormErrors;
导出默认类EmployeeRegister扩展React.Component{
构造函数(){
超级();
此.state={
empFirstName:'
}
this.handleChange=this.handleChange.bind(this);
this.clearFields=this.clearFields.bind(this);
}
手变(活动){
const target=event.target;
常量值=target.value;
const name=target.name;
this.setState({[name]:value})
});
}
clearFields(){
this.setState({empFirstName:''})
}
render(){
返回(
雇员登记表
名字:
);
}
};

此处的
名称
类型为
常量
。现在将
const
更改为
var
。应该行得通

 var name = target.name;
        this.setState({[name]: value})
        });

欢迎光临@B.阿伦