Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you pass in a dynamic form name in redux form?

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?

like image 277
jrutter Avatar asked Nov 09 '16 14:11

jrutter


People also ask

How do I initialize a redux form?

To get those values into your redux-form -decorated component, you will need to connect() to the Redux state yourself and map from the data reducer to the initialValues prop. By default, you may only initialize a form component once via initialValues .

Can I use redux form without redux?

1 Answer. Show activity on this post. Generally, it's expected that something with 'redux' in title won't work without Redux, otherwise this would be a terrible name for a package. If a package is advertised to be useable with Redux but isn't limited to it, its name doesn't contain 'redux'.


2 Answers

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.

like image 123
Parameshwar Ande Avatar answered Sep 24 '22 17:09

Parameshwar Ande


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!

like image 45
jpdelatorre Avatar answered Sep 24 '22 17:09

jpdelatorre