Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access a Field value with Redux-form - dependent Fields

I have a decorated component with redux-form HOC and I want to access a Field value from the decorated component to enable/disable and hide/show other fields. what's the best approach to do that?

I tried to use Fields component to operate in the dependent fields but that hurts the performance of the decorated component as it provokes useless re-renders

It is also possible to connect the decorated component with redux and use formValueSelector that is provided by redux-form, but I wonder if there is a better approach to access a field(s) value.

like image 931
Hakim Avatar asked Dec 01 '25 07:12

Hakim


1 Answers

Form Selectors and Field-dependent Values is described here. The solution is based on getFormValues:

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

import FormComponent from './form.component';

export const FormContainer = props => {
  const submitForm = (formValues) => {
    console.log('submitting Form: ', formValues);
  }

  return (
    <FormComponent
      formValues={props.formValues}
      change={props.change}
      onSubmit={submitForm}
      handleSubmit={props.handleSubmit}
    />
  );
}

const mapStateToProps = state => ({
  formValues: getFormValues('my-very-own-form')(state),
});
const formConfiguration = {
  form: 'my-very-own-form',
}

export default connect(mapStateToProps)(
  reduxForm(formConfiguration)(FormContainer)
);

and in your formComponent you can get the formValues from the props:

export const FormComponent = ({ handleSubmit, onSubmit, formValues }) => {
}
like image 133
kgiannakakis Avatar answered Dec 02 '25 23:12

kgiannakakis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!