我基于 Material-UI 中的组件创建了表单。我想控制state
来自父组件的输入,如下所示,但出现错误“不允许重复道具”。是的,这对我来说很有意义——我有 3 倍的onChange
参数。我很好奇是否有可能解决它或者我需要完全安排代码?
父组件
handleSubmitTask = (e) =>{
alert()
});
}
handleTextFieldChange = (event) =>{
this.setState({
nameTask: event.target.value,
});
}
handleCloseEditDialog = () => {
this.setState({openEdit: false});
}
handleChangeDate = (event, date) => {
this.setState({
deadline: date
});
}
handleChangeSelectField = (event, index, priority) => {
this.setState(
{priority}
);
}
render() {
const actionsEdit = [
<FlatButton label="Edit" primary={true} keyboardFocused={true} onClick={e => this.handleSubmitTask(e)} />,
<FlatButton label="Cancel" primary={true} onClick={this.handleCloseEditDialog} />
];
return (
<div>
<EditTaskDialog open={this.state.openEdit} actions={actionsEdit}
nameTask={this.state.nameTask} priority={this.state.priority} deadline={this.state.deadline}
onChange={e => this.handleTextFieldChange(e)} onChange={this.handleChangeDate} onChange={this.handleChangeSelectField}
/>
</div>
);
}}
export default Home;
子组件:
render() {
return (
<form>
<Dialog title="Edit your Task" open={this.props.open} actions={this.props.actions}>
<TextField floatingLabelText="Task" value={this.props.nameTask} errorText={this.state.nameTaskError}
onChange={this.props.onChangeText} onKeyPress={this.handleKeyPress}/>
<DatePicker floatingLabelText="Deadline" value={this.props.deadline} onChange={this.props.onChangeDate} />
<SelectField floatingLabelText="Priority" value={this.props.priority} onChange={this.props.onChangeSelect}>
<MenuItem value="High" primaryText="High" />
<MenuItem value="Medium" primaryText="Medium" />
<MenuItem value="Low" primaryText="Low" />
</SelectField>
</Dialog>
</form>
);}}