Skip to main content

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? (36 h 00 min)

You also get:

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

Already a member? Login here

Julian Grisales avatar

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.

👍 6
augusto-dmh avatar

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))
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.