【发布时间】:2018-12-25 22:01:32
【问题描述】:
我有一个表单,为了简单起见,我在下面发布了其中大部分内容,我需要使用 redux 存储中的值进行初始化。
我已经将 redux 存储中的 props 映射到组件(在 props.initialValues 中),但它不会设置表单的初始状态。
此外,当我尝试在表单的文本框中输入内容时,它不会让我输入任何内容。
我正在使用来自@material-ui/core 的表单元素。想看完整代码没有删减,可以查看here。
如何初始化表单中的值?
class FormSpellEdit extends Component {
constructor(props) {
super(props);
this.state = {
Name: 'NoName',
School: 'NoSchool',
Subschool: 'NoSubschool',
};
}
render() {
const {classes} = this.props;
const renderTextField = ({
input,
label,
...custom
}) => (
<TextField
hintText={label}
label={label}
floatingLabelText={label}
{...input}
{...custom}
/>
);
return (
<form
onSubmit={this.onSubmit}
initialValues={this.props.page.spell}
>
<Grid fluid>
<Row>
<Col xs={4} >
<Field
fullWidth
initialValue={this.state.Name}
name="Name"
component={renderTextField}
label="Spell Name"
/>
</Col>
<Col xs={4} >
<Field
fullWidth
name="School"
component={renderTextField}
label="Spell School"
/>
</Col>
<Col xs={4} >
<Field
fullWidth
name="Subschool"
component={renderTextField}
label="Spell Subschool"
/>
</Col>
</Row>
</Grid>
</form>
);
}
}
const mapStateToProps = (state, props) => {
return {
user_id: userId(state),
initialValues: state.spell,
formValues: getFormValues(props.form)(state),
}
};
const mapDispatchToProps = (dispatch) => {
return {
}
};
export default compose(
reduxForm({
form: 'SpellEditForm',
}),
connect(
mapStateToProps,
mapDispatchToProps,
),
withStyles(styles, {
withTheme: true
},
))(FormSpellEdit);
【问题讨论】:
标签: reactjs redux material-ui redux-form redux-thunk