Search code examples
reactjsreact-reduxredux-form

Can i connect redux-form [v6] to other state, like in [v5]?


Can i configure mapStateToProps and other react-redux parameters in redux-form v6.

Like this in redux-form v5 ~> https://i.sstatic.net/nSZKM.jpg

reduxForm(config:Object, mapStateToProps?, mapDispatchToProps?, mergeProps?, options?)

Creates a decorator with which you use redux-form to connect your form component to Redux. It takes a config parameter and then optionally mapStateToProps, mapDispatchToProps, mergeProps and options parameters which correspond exactly to the parameters taken by react-redux's connect() function, allowing you to connect your form component to other state in Redux.

.

For example:

import { reduxForm } from 'redux-form/immutable';

...

export default reduxForm({
  form: 'signUp'
}, state => ({
  lists: state.get('list').toJS()
}));

Solution

  • Yes, you can connect your redux state to your redux-form component in version 6. Here is an example (You have to decorate connect manually now).

    import { connect } from 'react-redux';
    import { reduxForm } from 'redux-form';
    import {
        actionCreatorOne,
        actionCreatorTwo
    } from './actionCreators';
    
    class MyForm...
    
    function mapStateToProps(state) {
        return {
            name: state.user.name
        };
    }
    
    let mapDispatch = {
        actionCreatorOne,
        actionCreatorTwo
    };
    
    MyForm = reduxForm({
        form: 'myForm'
    })(MyForm);
    
    export default MyForm = connect(mapStateToProps, mapDispatch)(MyForm);