24

Im trying to use this code to pass a dynamic form name into reduxForm.

Here is the code that I found:

let FormAddress = compose(connect((state, props) => ({form: props.form})), reduxForm({destroyOnUnmount: false, asyncBlurFields: []}))(ValidationForm);

From this article: https://github.com/erikras/redux-form/issues/603#issuecomment-254271319

But I'm not really sure whats going on.

This is how I'm currently doing it:

const formName = 'shippingAddress';
const form = reduxForm({
  form: formName
});

export default connect(mapStateToProps)(CityStateZip);

But I would like to be able to pass it in using props, like this:

const formName = 'shippingAddress';
const form = reduxForm({
  form: props.form
  // validate
});

export default connect(mapStateToProps)(CityStateZip);

But when I try that, it complains that it doesnt know what props is - because I believe it's outside the scope of the function above.

Can someone help me?

2
  • 2
    Personal suggestion here - but I've had much more success not using redux form. It is awesome at what it does but when you starting needing to add complexity from your app to it, it becomes difficult to work with. I know people have had great success with it, but something like the problem you are having here takes 2 seconds to do if you aren't depending on redux form...
    – ajmajmajma
    Nov 9, 2016 at 16:23
  • Just wondering if my answer worked for you Nov 24, 2016 at 23:11

2 Answers 2

43

The parent component which calling the FormAddress component should send the name of the form in props as props.form:

var formId="SomeId"
<FormAddress form={formId} />

// and in your FormAddress component have 
const form = reduxForm({
  //no need to put form again here as we are sending form props.
});

This works for me.

2
  • 1
    I like this because in my use case the name is really a concern of the parent rendering it. Now my form can be more general and does not need to derive its name. Dec 7, 2018 at 19:35
  • 1
    Nice and simple Feb 10, 2021 at 11:36
24

That snippet basically uses the compose function from redux library. Here's something you can try...

<FormAddress name="shippingAddress" />

So in your components/FormAddress.js file

import React from 'react';
import { compose } from 'redux';
import { connect } from 'react-redux';
import { reduxForm } from 'redux-form';

class FormAddress extends React.Component { ... }

const mapStateToProps = (state, ownProps) => {
    return {
        form: ownProps.name,
        // other props...
    }
}

export default compose(
    connect(mapStateToProps),
    reduxForm({
        //other redux-form options...
    })
)(FormAddress);

Hope this helps!

1
  • 1
    Upvoted this as a more complete answer, because ownProps as a parameter in mapStateToProps allows the user to access the specific name of the form from the redux store more consistently throughout the component using the "this.props" Jul 7, 2018 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.