Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Loading state and register form

Premium
6 min read

In this lesson, when a user submits the form, we are going to indicate if the form is being processed to give the user some feedback to have a more pleasant user experience.

Before submit:

Loading false

Right after the form is submitted and the client waits for a response:

Loading true

  1. By looking at the result we can immediately see that there is a new thing in the form, it is a loading indicator on the button.

Create a new component src/components/IconSpinner.jsx with the following content...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

JG
Julian Grisales ✓ Link copied!

Another way to see all your loading state is using the the dev tools of your browser in my case Chrome. Let me describe how it works.

  • Right clik on your browser and fin the option inspect.
  • Look for the Network tab
  • Go and select No throttling (left to the wifi icon)
  • Then select Slow 3G

This way you'll simulate an slow connection and you can see your loading states into your application.

When you finish, put again No throtting.

A
augusto-dmh ✓ Link copied!

Besides Julian's approach, there's still another way to emulate delay in operations, but for client-side (in an async function): Awaiting a Promise that resolves in a setTimeout callback.

    async function register(data) {
        setErrors({})
        setLoading(true)

        // delay of at least 5 seconds before registration
        await new Promise(resolve => setTimeout(resolve, 5000));

        return axios.post('auth/register', data)
            .then(() => {
              navigate(route('vehicles.index'))
            })
            .catch(error => {
              if (error.response.status === 422) {
                setErrors(error.response.data.errors)
              }
            })
            .finally(() => setLoading(false))
    }