【问题标题】:React Redux Form set initial value from store?React Redux Form从商店设置初始值?
【发布时间】: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


    【解决方案1】:

    尝试添加连接组件之前 reduxForm,名称必须匹配

    export default compose(
            connect(
                mapStateToProps,
                mapDispatchToProps,
    
            ),
            reduxForm({
                form: 'SpellEditForm',
            }),
    
            withStyles(styles, {
                    withTheme: true
                },
            ))(FormSpellEdit);
    

    【讨论】:

    • 这些名称确实与状态键名称和Name 名称的形式相匹配。我尝试将连接移到第一位,但没有任何改变。
    • 比尝试包装组件:github.com/erikras/redux-form-material-ui, branch 0.5
    • 尚未更新到 material-ui 1.0 版本。
    • 确实如此,但它与 material-ui 1.0 配合得非常好,我们在一些项目中使用
    • 是的,但是当我尝试时出现错误 > ./node_modules/redux-form-material-ui/lib/AutoComplete.js 未找到模块:无法解析 'material-ui/AutoComplete '在'/app/frontend/node_modules/redux-form-material-ui/lib'中
    猜你喜欢
    • 2017-10-24
    • 2019-07-11
    • 2019-10-13
    • 2016-09-30
    • 2017-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    相关资源
    最近更新 更多