Does redux-form field value can hold object instead of just a string?
Consider following example
class SelectQuestions extends Component{
render(){
const {fields:{question1,question2},handleSubmit}=this.props;
return <div>
<form onSubmit={handleSumbit(this.handleFunction.bind(this))}>
<SelectQuestion {...question1}/>
<SelectQuestion {...question1}/>
<button type="submit" className="btn btn-default">Submit</button>
</form>
</div>
}
}
class SelectQuestion extends Component{
render (){
<select></select>
}
}
'SelectQuestion' takes array of Questions, each Question has question id and question name.
Once the user selects the question I wanted to to return QuesionId and QuesitonName. How it can be achieved using redux-form in react
Yes, the value can be a complex object. There's even an example demonstrating it.
Complex Values Example
TL:DR
Yes it can hold an object. Check out this example: https://codesandbox.io/s/vq6k6195rl
Explanation:
For a select, you can define your selects like this:
Then wrap your component with Field component.
And then just show it in your render:
I think the better way to achieve that is to create two virtual fields
selectedQuestionId
andselectedQuestionName
. Those fields are not to display. Then, eachSelectQuestion
can trigger a callback like:onChange
can be called on redux-form fields to programmatically set the value.This is a bit late, but still..
Yes, it can! You can simply use a FormSection here.
It will split your form to an objects under the hood, and will keep all sections separtely in your single form object.
Just need to pass a
name
prop to the FormSection, that will be a name of your sub-object.Then check your form structure through the Redux devTools.
Enjoy ;)