⚠️ 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
Published 7 years ago
Updated 2 years ago

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.

egghead
egghead
~ 29 minutes ago

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

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

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!

Markdown supported.
Become a member to join the discussionEnroll Today