Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redux-form : How to display form values on another component

I am using redux-form 6.0.0-rc.5 and I am trying to display the form values as they are entered by the user.

However, I want these values to be displayed from another component, not the redux form itself.

So, my simplified App architecture would be like that :

<App />              -> (main component -container ?-)
  <List />           -> (connect to form values and pass them down)
    <Elem />         -> (display form value)
  <Form />           -> (enter form values)

The component is a redux-form mounted to 'form' and is working.

Form = reduxForm({
  form: 'formName'
})(Form)

What would be a good way of getting the form values (from state form.formName.values) and send them to my Display component ?

Things I have tried :

  • Connect App to the store and mapStateToProps (form.formName.values) then pass it down to Display as a prop. But it throws an error since values does not exist in the form state until the user has typed anything.

  • Using the function getFormValues('formName') provided by redux-form inside the List component but it returns a function or undefined :

Elem

const Elem = ({ name }) => (
  <li>{name}</li>
)

List

const List = ({ values }) => (
  {values.map(value => <Elem name={value.name} />)}      
)

List = connect(
  state => ({
    values: getFormValues('formName')
  })
)(List)

There must be something I am missing or I still do not understand correctly whether it is with redux-form or redux itself... I hope someone will be able to enlighten me !

Thank you and have a great day.

like image 267
Thomas Milan Avatar asked Aug 19 '16 10:08

Thomas Milan


People also ask

How do I get values from Redux form?

To get them, you will need to connect() directly to the form values in the Redux store. To facilitate this common use case, redux-form provides a convenient selector via the formValueSelector API.

How do you pass a state from one component to another in Redux?

Passing State to a Component To pass the state into another component, you can pass it as a prop. Then, inside <ExampleComponent /> , you can access the data as this. props.

What is FieldArray in Redux form?

The FieldArray component is how you render an array of fields. It works a lot like Field . With Field , you give a name , referring to the location of the field in the Redux state, and a component to render the field, which is given the props to connect the field to the Redux state.


2 Answers

Try using

List = connect(
  state => ({
    values: getFormValues(state.form.formName)
  })
)(List)

instead. At least that's how it worked in v5, although there the method was called getValues and not getFormValues.

Edit: After a quick look at the docs it seems in v6 you'll have to use a formValueSelector: http://redux-form.com/6.0.0-rc.3/examples/selectingFormValues/

like image 60
tmaximini Avatar answered Nov 15 '22 08:11

tmaximini


formValueSelector() is not necessary.

You also can directly access it as a property.

List = connect(
  state => ({
    formValues: {
       id: state.form.formName.values.id
    }
  })
)(List)

Same as

List = connect(
  state => ({
    formValues: {
       id: formValueSelector('formName')(state, 'id')
    }
  })
)(List)
like image 25
Julha Avatar answered Nov 15 '22 09:11

Julha