Redux Form: How to handle multiple buttons?

后端 未结 2 1250
时光取名叫无心
时光取名叫无心 2020-12-15 05:30

I am trying to add a second submit button to a redux-form.

Both buttons should dispatch an action that saves the data but depending on the button pressed the user sh

2条回答
  •  情歌与酒
    2020-12-15 06:20

    @mibbit onSubmit is a function that you define (at least this is what the doc says: look the onSubmit method). This is for redux-form 7.x following example of @Erik R.

        class Morpheus extends Component {
        constructor(props) {
            super(props);
    
            this.state = {};
            this.onSubmit = this.onSubmit.bind(this);
        }
    
        onSubmit(values, pill) {
            // do magic here
        }
    
        render() {
            const {
                handleSubmit
            } = this.props;
            return ( <
                div >
                Fields go here <
                button onClick = {
                    handleSubmit(values =>
                        this.onSubmit({
                            values,
                            pill: 'blue'
                        }))
                } > Blue Pill < /button> <
                button onClick = {
                    handleSubmit(values =>
                        this.onSubmit({
                            values,
                            pill: 'red'
                        }))
                } > Red Pill < /button> <
                /div>
            );
        }
    }
    
    export default reduxForm({
        form: 'morpheus'
    })(Morpheus)
    

提交回复
热议问题