如何使forceUpdate()工作以重新呈现我的WIzard表单?

问题描述 投票:1回答:1

Noob问题。我有一个WizardForm有两个提交按钮。它在第三步提交数据,然后在第四步上传照片。如何在照片上传成功后重新呈现整个表单?我正在尝试回到表单的干净状态,以便用户可以继续再次输入数据,就像任何正常的数据输入表单一样。

这是我的代码:

import React from 'react';
import {Field, reduxForm} from 'redux-form';
import validate from '../middleware/validate';
import { post } from 'axios';
import {BASE_URL} from '../middleware/api';
import {connect} from "react-redux";
import WizardForm from './WizardForm';

let WizardFormPhoto = (props) => {
const {handleSubmit, pristine, previousPage, submitting} = props;
const onFormSubmit = (data) => {
    const {reset} = this.props;
    let humanID = localStorage.getItem('humanID');
    let token =localStorage.getItem('idToken');
    const AuthStr = 'Bearer '.concat(token);
    let formData = new FormData();
    formData.append('humanId', humanID);
    formData.append('photo', data.profile_pic[0]);

    const config = {
        headers: {'content-type': 'multipart/form-data', 'Authorization' : AuthStr}
    };
    const url = BASE_URL + 'human/upload';

    post(url, formData, config)
        .then(function (response) {
            alert(response.data.message);
            reset();
            WizardForm.forceUpdate();
        }).catch(function (error) {
            console.log(error);
        });
};
return (
    <form onSubmit={handleSubmit(onFormSubmit)} className="form-horizontal">
        <div className="step-3">
            <div className="form-group">
                <label className="col-sm-2 control-label">Add Photo</label>
                <div className="col-sm-10">
                    <Field name="profile_pic" component="input" type="file"/>
                </div>
            </div>
            <div className="clearfix">
                <button type="submit" className="next pull-right btn btn-primary">Submit</button>
            </div>
        </div>
    </form>
);
};


export default reduxForm({
form: 'wizard', //                 <------ same form name
forceUnregisterOnUnmount: true, // <------ unregister fields on unmount
validate,
})(WizardFormPhoto);

调用foreceUpdate()给了我以下错误WizardForm__.a.forceUpdate is not a function。我怎样才能让它发挥作用?

P.S:如果重要的话,WizardForm是母组件。

添加WizardForm

 import React, {Component} from 'react';
 import PropTypes from 'prop-types';
 import {connect} from "react-redux";
 import {reduxForm} from 'redux-form';
 import WizardFormFirstPage from './WizardFormFirstPage';
 import WizardFormSecondPage from './WizardFormSecondPage';
 import WizardFormPreview from './WizardFormPreview';
 import WizardFormPhoto from './WizardFormThirdPage'
 import {
isSubmitting,
hasSubmitSucceeded,
hasSubmitFailed
} from 'redux-form'

class WizardForm extends Component {
constructor(props) {
    super(props);
    this.nextPage = this.nextPage.bind(this);
    this.previousPage = this.previousPage.bind(this);
    this.backToOne = this.backToOne.bind(this);
    this.state = {
        page: 1,
    };
}

nextPage() {
    this.setState({page: this.state.page + 1});
}

previousPage() {
    this.setState({page: this.state.page - 1});
}

backToOne() {
    this.setState({page: 1,})
}

render() {
    const {onSubmit} = this.props;
    const {page, submitSucceeded} = this.state;
    return (
        <div>
            {page === 1 && <WizardFormFirstPage onSubmit={this.nextPage}/>}
            {page === 2 &&
            <WizardFormSecondPage
                previousPage={this.previousPage}
                onSubmit={this.nextPage}
            />}
            {page === 3 &&
            <WizardFormPreview
                previousPage={this.previousPage}
                onSubmit={values => {
                    onSubmit(values, () => {
                        this.setState({
                            submitSucceeded: true
                        });
                        this.nextPage()
                    });
                }}
            />}
            {submitSucceeded && page === 4 &&
            <WizardFormPhoto onSubmit={onSubmit}/>
            }
        </div>
    );
}
}

WizardForm.propTypes = {
onSubmit: PropTypes.func.isRequired,
};

WizardForm = reduxForm({
form: 'wizard',
initialValues: {
    location: {
        latitude: "0.0",
        longitude: "0.0"
    }
    }
})(WizardForm)

WizardForm = connect(
state => ({
    submitting: isSubmitting('wizard')(state),
    submitSucceeded: hasSubmitSucceeded('wizard')(state),
    submitFailed: hasSubmitFailed('wizard')(state)
})
)(WizardForm)

export default WizardForm;
javascript reactjs redux axios redux-form
1个回答
0
投票

您是否尝试使用intializeForm动作创建者,如下所示?

import { initialize as initializeForm } from 'redux-form';

dispatch(initializeForm('wizard', {}));
© www.soinside.com 2019 - 2024. All rights reserved.