React Native, v2

Form Solution: Adding a FlatList

Kadi Kraman

Kadi Kraman

React Native, v2

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

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

Kadi live codes the last part of the solution to the form exercise by adding a FlatList component that allows the user to map over the different color schemes and select the color wanted. This section demonstrates how to use React hooks for forms and navigation within React Native.


Transcript from the "Form Solution: Adding a FlatList" Lesson

>> So now we are gonna need to render lots and lots and lots of colors. So let's just go ahead and copy the giant colors array. And add it here. And because there's lots and lots of lots of things, we're going to use a FlatList. Let's go underneath Palette name and do FlatList.

So here our data is the COLORS. keyExtractor will be item => item.colorName. Then renderItem, going to get the item which is the individual color. And now we can pretty much just copy this, the switch code that we made earlier, And put it in here. So here the color name will now be item.colorName, And for the styles, do I have to change anything?

Cool, now we've got an array of all the colors. So next what we want to do, we can actually remove some of this padding now, some of this margin now, okay. So the next thing you wanna do is, obviously, these are all turned on by default. We want some way to actually track what's turned on and what's off.

So in order to do this, let's create a new state variable to hold this, so let's create one for selected colors only. So let's do SelectedColors and setSelectedColors, useState and let's start with an empty array. Now, we want this selectedColors array basically to contain, they each contain one of these.

So here, where we decide where something is selected. So this value is either true or false based on whether it's selected or not. So all we can do is we can do if selectedColors.find, so this is color, which is individual item. So color.colorName is the same as this item, colorName, and it's true.

And basically, what we want here is we want to force this into being a Boolean, and a handy way to do that is a double exclamation mark. So a single exclamation mark would make this the opposite of what it was before. So if it was true, it would be false.

If it was an object, it would be false. And then a double one will just basically converted into a Boolean and then convert it back. So you make it truthy. Cool, so now all of these are not selected, which is correct, cuz we don't have any items in our array.

And last thing we need to wire up is this onValue change. So let's create another callback. Let's call it handleValueChange, And here, we get two things. So we get the value, so whether the user turned this thing on or off. So value and also we get the whole color.

Nice, so here basically if value is true. So this means that the user has toggled the button on, we want to add the color into our selected colors array. So let's do setSelectedColors. And we do care what the current ones are. So we do colors. And we return all the colors, but with the current one added.

And in this case, we don't really, do we care? I guess I'm adding it to the end of the color list, I guess it doesn't matter too much. And then otherwise, if the value is false, we know that the user has toggled it to be off. And in this case, we are going to remove it from our current color array.

So we do setSelectedColors, And then we're just going to filter the color that they deselected out. Actually, I can't call this color. I've gotta call it selectedColor. And I'm going to do selectedColor.colorName. All right, so here it basically does colors.filter. And this filters out everything where the colorName is the selectedColorName.

In theory, well, okay, so the reason it doesn't work is we aren't actually calling this function, so we need to add it to onValueChange. So onValueChange will return selected, so this is the Boolean. And in this case, we do actually want to call this explicitly cuz we wanna pass in two things.

You wanna pass in selected, And also the color that we're currently working on. So this two arguments. There you go so, I think this might have to be a converse. Okay, yeah, so I had a small mistake here when I did a filter, I filtered everything out rather than this.

So if you followed along, make sure this is a negation. And now I can select all of these. Now, if you remember, we had another alert that we had to do. We had to check that there are at least three selected colors, so let's add another if else here.

else if, let's do selectedlColors.length, let's say, is less than 3. Then we do an alert saying, Please add at least 3 colors. So if I do have the name, I press net. Why does this still work?
>> I think it's misspelled.
>> Thank you, misspelled length. All right, so add a name and add no colors.

Okay, so it's saying please add at least three. Doesn't work with two, but if I add three-
>> You need selected colors in your array at the bottom.
>> Yes, yes, I do, thank you. This is because our handle submit hook is now being re-rendered. I told you this happens to me all the time [LAUGH].

All right, let's actually add some interesting colors here. Why did these not show up? I know why, because I've hard coded this colors array to be empty. So let's do selectedColors here. And I'm just gonna refresh. Yay, it works! That's pretty cool. And so there's loads and loads of things where you can enhance this.

You could make the Submit button nicer, you could add little color previews, yes?
>> I was just gonna say that's what I did. I had a blue square of color next to the drawing, so.
>> Nice.
>> None of them actually are.
>> Yep, perfect, yeah, so there's loads of little things that you can do.

You can enhance the net hook request, they actually do a post. If you wanted to, you could add the bottom navigation. The API actually has a search functionality. So if you search by ID, you can also preview a particular color, so there's loads of things if you want to tinker a bit.

Sorry, you had a question?
>> You mentioned in the beginning that React Native has flex out of the box. Does it also support grids?
>> No, I don't think so. I think it's flex only, yep.
>> So you cannot even wire that up?
>> I don't think so, sorry, yes?

>> What's the benefit of using use callback as opposed to just writing another anonymous function?
>> You could write an anonymous function. Yeah, especially so if you have, if you had a use callback and you put it outside of here, that will be completely fine to do. You just have to make sure to pass in all the arguments into the function.

So you can do either. I'm pretty sure I could just, yeah, so I could move this handleSubmit, for example, outside of the function. But then every time I call it, I would have to pass in the selected colors. And I would have to pass in the currently selected name as well.

So you could do it, but you need to make sure you pass in all the different arguments, cuz it will be a pure function.

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