React Native, v2

Form Exercise

Kadi Kraman

Kadi Kraman

React Native, v2

Check out a free preview of the full React Native, v2 course

The "Form Exercise" Lesson is part of the full, React Native, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to code a form that allows users to create a color scheme. The form should accept a text name, colors using toggle buttons. A button allowing the submission of the form should also be added.


Transcript from the "Form Exercise" Lesson

>> Last exercise and this is definitely going to be the trickiest one. If you're going to take some time and have a go at it yourself, don't be alarmed if it takes you an hour or longer because this is meant to be quite a, there's a lot going on there.

So here's a little recording of what the end product would look like. We've got an add color scheme button which is similar to what we already have. And then this launches a Moodle. And in the Moodle we have an input where you can name your color palettes. And then we have a list of loads and loads of colors.

And you can toggle these on. And finally, we submit the color and it gets added at the top of our list. So I've added some requirements here, so you can basically work on these one by one. Firstly, pressing on add color scheme opens the Moodle, we've pretty much done that already so you get that one for free.

Secondly, the user can enter the name for the color scheme. Here you can add name. Thirdly, the user can toggle the buttons to select colors to add the theme. Now, ideally, we'd show a little preview for the colors but I didn't want to go too crazy cause there was a lot going on already, and you can have a go at it on your own.

So each color just has a toggle button on the right and you can toggle that on and off. If the user has to submit without entering a name for the color, they will get an error message. So this is not actually demonstrated in the GIF. But if the, if I was to press Submit, without actually adding the name, it would give me an alert, saying, please add a name.

If the user hits Submit without entering the correct number of colors, which is three, I do get an error message. Should there is a check before you submit that if the user has selected two or fewer colors, it will say please select at least three. And finally, if the user has entered the name correctly, and have chosen at least three colors, the Moodle will close and a new color scheme will be added at the top of the list.

There's basically the requirements. I've added some hints here, if you're gonna have a go at this, don't use all colors all at once. I've added, so basically these are all the web colors. So you can start with just a couple and I just to figure out how the toggle button works and just getting that working, and then adding more once the basic is done.

For these bits, the submit warnings, you can use the native alert Moodle, which is here. You just call alert dot alert. With this you don't even need to add all these arguments. And this will tell the user that, something's wrong. This is the like cheapest way to do little alerts.

And finally, navigation, navigate works just like navigation go back if the page you target is already on the stack. So if I'm here inside the Moodle, and then, if I do navigation go back, would go back to the home. But similarly, if I did navigation, Navigate Home, it would do exactly the same thing.

And this is handy because you can pass in arguments into navigate whereas you can't pass in arguments into go back.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now