简体   繁体   中英

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

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

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'))

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM