- Overview
- Transcript
5.3 Display a Deck of Cards
Now that we have cards, let's display them. We started the VisibleCards
component several lessons ago—now it's time to finish it, so that we can view the cards in a deck.
1.Introduction2 lessons, 04:39
1.1Introduction01:31
1.2Application Demo03:08
2.Get Started With Redux6 lessons, 41:43
2.1Set Up the Project09:34
2.2Reducers and Actions10:10
2.3Combining Reducers08:42
2.4Challenge: Add a Case to a Reducer04:44
2.5Challenge: Split a Reducer05:19
2.6Challenge: Build a Component03:14
3.Create React Components8 lessons, 50:18
3.1Build a Pure Component04:53
3.2Start the Sidebar04:31
3.3Write Action Creators08:37
3.4Use Action Creators06:42
3.5Challenge: Temperature Converter05:50
3.6Challenge: Todo List09:21
3.7Challenge: Action Creators07:26
3.8Challenge: Refs Research02:58
4.Application Structure9 lessons, 56:09
4.1Refactor Our Application for Growth08:43
4.2Using the `react-redux` Package13:12
4.3Add a Router07:24
4.4Create Nested Routes07:44
4.5Add `localStorage` Support03:38
4.6Challenge: Presentational and Container Components07:26
4.7Challenge: Basic Routing02:53
4.8Challenge: Route Not Found02:51
4.9Challenge: Route Parameters02:18
5.Implement the App9 lessons, 1:31:34
5.1Create the Toolbar06:16
5.2Create the New Card Modal15:16
5.3Display a Deck of Cards05:27
5.4Create the Edit Card Modal10:20
5.5Filter Cards06:24
5.6Create a Study Interface19:29
5.7Add Asynchronous Actions13:08
5.8Challenge: General Conversion Component07:11
5.9Challenge: Users List Component08:03
6.Conclusion1 lesson, 01:32
6.1Conclusion01:32
5.3 Display a Deck of Cards
Now that we can create flash cards the next step is being able to display them. Now we are already have our visible cards component, but right now it doesn't really display anything. So let's expand this component so that it will display whatever cards are in the currently selected deck. The first and most obvious step I think is that this props object that is passed to the cards component function here will take more than just the children as a parameter. It's also going to have a property called cards and this is going to be the list of card objects that we want to display. So then inside this div. Let's expand this a little bit, and let's see. First thing I wanna do is add a class name to this div, will just say main, and now inside here right now we just have the text deck will display here and children. Let's get rid of the text deck will display here and we'll leave the children. But then above the children, we're going to display our cards here. And this is going to be very simple. We'll just say cards.map. And we're going to map over our cards. So for each card, what we'll return is a new card element. Now we're going to create this component in just a second here but this card component is what is actually going to display each one of our cards. And we'll make it very easy here, we'll just set the card equal to card. And we'll set the key. Equal to card.id. Now remember because we're going to be displaying multiple card elements beside each other here. We need to give each one a key and so we'll just use the unique ID as the key for that card that's actually all we need for the cards object. However there are a few other pieces to put into this puzzle. Let's go ahead and import card from the card file. We haven't created that yet but we'll do that very soon. Now the other thing of course is the connect function. So let's import connect from react-redux and now, let's make our map state to props function. We know that this function will get two parameters, the props and the router, and this is the first container component that's actually going to need something from both of those. From the props object here, we're just going to need the cards property. So let's destructure this and just get the array of cards that we have. And then for the rotor here. Let's de-structure this and we need params and then we need deck ID. Just as we've done before. Inside the object that we return from map state to props. We know that we need a cards array and let's start with the current cards array, but then we're going to do some filtering. So for each card we'll only keep the ones where card.deckId is equal to the deckId that we get in the URL. So this is how we can choose exactly which cards to display at any given time. when we click on one of our list names on the deck sidebar the deck ID will be set and then we can use that to filter through our list of all cards. Now the only thing to do is connect these parts. So let's go ahead and say export default connect and we'll pass that map state to props and then to that function. We will pass cards and now we have our completed. visible cards component of course we can't quite see this yet because we haven't created our card component yet. So let's create the file source slash components slash card. J.S. well go ahead and import react and we're also going to import link from the react rotor because each individual card will need to have a link to the edit modal. All right so let's go ahead and create our card component. This is going to be a function. And as we already saw it's going to take a single property and that will be the card. Again of course, we're destructuring from the actual props object in here. Let's go ahead and return to Dave and we'll give this a class name of card then inside this let's put another day of and this is going to wrap a paragraph and this paragraph is going to have card front. So when our cards are listed in the main area of our application will be able to see whatever the front of the card might be and then underneath this, let's put a link, and this link will just have the text edit, and we will have to give it a few properties. Let's see. Well give it a class name of button so that it looks like a button, and then we'll set this to, and again, we'll use the string template syntax, and we'll go to /deck/, and then let's interpolate card .deckId/edit and then forward slash and then we'll integrate card.id. So the whole URL is deck/deckId/edit/cardid. Right that's actually everything we need. So let's go ahead and say export default card and we should be good to go. If we had back to the browser you can see that right now we have no deck selected. If I click on deck one over here, however you can see the deck one becomes visible when we click that. So, we have two cards that we've already made in this deck and they are now visible. Of course we also have the toolbar for deck tools that we previously made, but now we can actually see the cards that we have in our deck. If I move over to tech to you can see that because of our filter, we're showing no cards because there are none that belong to deck two. Let's add a new card. And if I say this you can see that this card shows up for deck two we can go back to deck one and now we have the deck one cards. Excellent and you can see when I have her over a card our edit button appears. I can go ahead and click that edit button and notice that the U.R.L. changes to deck deck ID EDIT EDIT ID and of course we don't have a route to match that right now but will take care of that in the next lesson.