Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redux-form -- Set Value for a field via code?

Tags:

reactjs

redux

I'm creating a customised component for my form.

What I want: after I click a certain location, in my onClick handler, I can set the corresponding value for the form field.

How to do that?

My current solution:

_onClick(value, evt) {
  const {field, dispatch} = this.props
  if(dispatch){
    dispatch({type: "redux-form/CHANGE", field: field.name, value: value, touch: false, form: field.form})
  }
}

it doesn't work yet.. but even if it works, I feel this is kind of hack.

Any better solution?

Note: I also asked this question on the issue page for redux-form: https://github.com/erikras/redux-form/issues/369

like image 293
songyy Avatar asked Dec 02 '15 06:12

songyy


2 Answers

Redux Form 6

Inside the form component using props

this.props.change('field_name', 'value')

Outside the form component using Action Creators

import { change } from 'redux-form'

dispatch(change('form_name', 'field_name', 'value'))
like image 170
Jakub Zawiślak Avatar answered Oct 12 '22 14:10

Jakub Zawiślak


Issue resolved. It turns out that I should make my component compatible with Redux-Form, reference: http://erikras.github.io/redux-form/#/faq/custom-component?_k=qnjmi9

like image 22
songyy Avatar answered Oct 12 '22 12:10

songyy