⚠️ This lesson is retired and might contain outdated information.

Set an Initial Value of a Form Field using redux-form

Rory Smith
InstructorRory Smith
Share this video with your friends

Social Share Links

Send Tweet

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.