Join egghead, unlock knowledge.

Want more egghead? It's 45% off for a limited time only!

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock All Content for 45% Off

Already subscribed? Sign In

Save 45% for a limited time.

Get access to all courses and lessons on egghead today.

Autoplay

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

    Rory SmithRory Smith

    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.

    reactReact
    ^16.0.0
    reduxRedux
    ^3.0.0
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    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.

    Discuss

    Discuss