我在反应中实现表单输入验证时遇到了问题。一旦我打开表单,总是有 [object object] 作为默认输入出现。这是显示:
这是我的代码,知道如何修复它吗?
const ErrorValidationLabel = ({ txtLbl }) => (
<label htmlFor="" style={{ color: "red" }}>
{txtLbl}
</label>
);
const txtFieldState = {
value: "",
valid:true,
}
class Setting extends Component {
constructor() {
this.state = {
name: {...txtFieldState, fileName:"name", required:true, requiredTxt:"Device ID is required"} ,
}
this.handleNameChange = this.handleNameChange.bind(this);
this.handleNameSearch = this.handleNameSearch.bind(this);
handleNameChange(event) {
this.setState({ name: event.target.value });
}
handleNameSearch(event) {
//http request call logic
}
}
render() {
const renderNameError = this.state.name.valid? "" : <ErrorValidationLabel txtLbl={this.state.name.requiredTxt} />;
return (
<form onSubmit={this.handleNameSearch}>
<label >
Enter Your Device Id:
</label>
<Input name="name" type="text" placeholder="ex:12345678910" value={this.state.name} onChange={this.handleNameChange} required/>
{renderNameError}
<Input type="submit" value="Search Device" />
</form>
);
}