2017-06-22 54 views
1

我得到这样的错误在我ReactJS应用,遗漏的类型错误:无法创建于字符串“财产'empFirstName”

遗漏的类型错误:无法创建属性“”上线“”

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> 



     ); 
    } 
}; 

回答

1

name这里是const类型。现在将const更改为var。它应该工作

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

是的,得到它的感谢。 –

+0

欢迎。 @ B.Arun – Ved

相关问题