首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用redux表单提交表单

使用redux表单提交表单
EN

Stack Overflow用户
提问于 2018-07-21 09:03:25
回答 0查看 738关注 0票数 0

我有我自己的动作,我想用它来提交一个redux表单

actions/spell.js

代码语言:javascript
复制
export const updateSpell = (spell) => ({
    [RSAA]: {
        endpoint: '/api/spell/' + spell.id,
        method: 'PUT',
        body: JSON.stringify(spell),
        headers: withAuth({ 'Content-Type': 'application/json' }),
        types: [
            UPDATE_SPELL, UPDATE_SPELL_SUCCESS, UPDATE_SPELL_FAILURE
        ]
    }
});

但是我已经完全弄清楚了如何设置提交函数。我已经尝试了我在网上搜索的各种解决方案,但它们给出了各种错误。

默认操作不是我希望表单具有的行为。尝试替换我自己的提交函数,它要么抛出与redux-form应该如何设置相关的错误,要么我不知道如何传递表单值。

关于如何设置redux表单的自定义提交功能,有什么指导吗?

代码语言:javascript
复制
class FormSpellEdit extends Component {
    constructor(props) {
        super(props);

        this.state = {
            id: 0,
            owner: 0,
            Name: 'NoName',
            School: 'NoSchool',
        };
    }

    componentDidMount() {
        this.props.initialize(this.state)
    }

    render() {
        const { classes, submit, handleSubmit, pristine, reset, submitting } = this.props;

        const renderTextField = ({
                 input,
                 label,
                 meta: { touched, error },
                 ...custom
             }) => (
            <TextField
                hintText={label}
                floatingLabelText={label}
                errorText={touched && error}
                {...input}
                {...custom}
            />
        );

        return (
            <form
                onSubmit={handleSubmit}
            >

                <Button
                    variant="fab"
                    color="primary"
                    aria-label="Save"
                    disabled={pristine || submitting}
                    onClick={submit}
                >
                    <SaveIcon/>
                </Button>


                        <Grid fluid>
                            <Row>
                                <Col xs={12} >
                                    <CardContent className={classes.spellCardContent}>
                                        <Typography>Spell Name</Typography>
                                        <Divider />
                                        <Field
                                            fullWidth
                                            name="Name"
                                            component={renderTextField}
                                            label="Spell Name"
                                            value={this.state.Name}
                                        />
                                    </CardContent>
                                </Col>
                                <Col xs={12}>
                                    <Divider />
                                </Col>
                                <Col xs={6} lg={1}>
                                    <CardContent className={classes.spellCardContent}>
                                        <Typography>School</Typography>
                                        <Divider />
                                        <Field
                                            fullWidth
                                            name="School"
                                            component={renderTextField}
                                            label="Spell School"
                                        />
                                    </CardContent>
                                </Col>
                            </Row>
                        </Grid>
        );
    }
}


const mapStateToProps = (state, props) => {
    return {
        errors: authErrors(state),
        user: state.auth.access,
        user_id: userId(state),
        page: {
            spell: state.spell
        },
        initialValues: state.spell,
    }
};

const mapDispatchToProps = (dispatch) => {
    return {
        handleSubmit: (values) => dispatch(updateSpell(values)),
    }
};


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

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51451960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档