We can set initial default values to form fields in redux-form by using the initialValues
prop passed into the form component. Let’s find out how this is useful to improving the user experience of our form.
Instructor: [00:01] There may be situations in which we want to set initial values for fields in our form. In redux-form, we can achieve this by passing in an initial values prop to our form component.
[00:16] Let's pass initial values in as a prop to register form. We're going to pass this.get initialValues. Let's write get initialValues, and we're going to return an object containing some initial values for our form.
[00:37] To do this, we need to specify the name of the form field. If we look in our register form, we have name, preference, and newsletter. In our initial values, let's return an object. The object keys will correspond to the names of our form fields. Let's say preference is spaces by default, and newsletter is true by default.
[01:11] Then, let's head over to our fields, and we can remove this empty option from our custom select component. Let's save and refresh. Now we have the initial value set up. Preferred formatting is set to spaces and sign up to newsletter is set to true.
Member comments are a way for members to communicate, interact, and ask questions about a lesson.
The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io
Be on-Topic
Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.
Avoid meta-discussion
Code Problems?
Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context
Details and Context
Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!