- Overview
- Transcript
3.2 Start the Sidebar
Our next component will be a sidebar for our app. We’ll build this one with a React class, using the React.createClass()
function.
Related Links
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
3.2 Start the Sidebar
The app component that we built in our previous lesson didn't do very much, because it's just going to be a container that's gonna hold everything else. Let's work on a more visible component in this lesson, the sidebar. It's easier to build components as pure functions, but this component is not going to be a pure function. It's going to be an actual react class. Now the reason we're doing this is because this component needs to use some of react's lifecycle methods. These methods allow you to perform certain actions at certain points in the lifecycle of the component. For example, we have functions like component will mount or component did mount or should component update. Things like these allow you to control how the component acts as the state as the state of the component changes. Now we won't actually be using any of these lifecycle methods just yet, but we'll get there soon. So let's create our Sidebar component. We can start by saying const Sidebar, and this is going to equal React.createClass. This takes an object, and this object is going to have a render function. So this render function is kind of like our pure functional component, it just returns the JSX that we need. Now the difference here is that we don't get the props as a parameter to render, Iinstead we have a props property on our component object. So, I'm just going to say let props = this.props. Kind of a shortcut, so we don't have to write this top props every single time. Now we can actually return our sidebar. So let's create a div element here, and I'm going to give it a class slidebar in here. Let's create an h2, and we'll just have a label at the top saying All Decks, and then underneath this let's create our own ordered list of decks. And then, inside the unordered list, we will loop over our decks. Now, we get our decks list as one of the properties passed to this component. So, we'll say props.decks.map. This will take two parameters. We'll take the deck, and we also want the index, because we're gonna use the index as the key for our list items. So, in React, whenever you have Sibling elements, for example, number of list items inside of an unordered list. When you have a bunch of sibling elements that are the same element like that they need to have a key, an unique key, so the react can tell them apart. So we will just use I as our key value, and then inside of this will say deck.name. And that will just be the text of the list item for now. Now there's one more thing we want to add to this sidebar, and that is a text box that allows us to add a new deck. However, we only want this to be showing if the adding deck state property is true. And we don't have a reducer for this yet, we haven't added the adding deck property to our state. However, we're gonna get to that soon enough. For now, let's just assume that our side bar receives props.addingDeck. And if that's true then we want to go ahead and display the text box where they can type in the decks name. Now we can kind of take a shortcut here and use the binary and operator. If we say props.addingDeck, if this is true then we'll go ahead and render whatever comes after our double ampersand here. However if this is false, the whole statement will be considered false and nothing will be rendered. So here we can just have an input and for now we'll just give it a react reference called add, and we'll come back to this element a little bit later. But for now we should be able to see this in action. So let's go ahead and render our Sidebar inside of our app component here. So we'll get rid of our previous content inside the app component, and let's render a side bar, and we'll have to give it a Dex property and for now that can just be an empty array. Actually when we put something in there, so we can see something all we need to do is give it a name element, and we'll say Deck 1 and we also need to give our Sidebar a property AddingDeck and for now that can be false. All right, so we should be able to see our side bar now. If we come back to the page you can see that now we have our sidebar showing up on the left here, we have our all decks header and then we have deck 1 showing up. Now our text box does not show up, because addingDecks is false. However, if we come back to where we're rendering the sidebar, and we change addingDeck to true, you can see that now when the page refreshes we have our text box displaying. So, that is our basic sidebar component. Now, right now this component of the sidebar doesn't actually interact with our store or state at all. So that's what we're gonna do in the next lesson. We're going to take the two sides of our application that we've created, our store for managing data, and our react components for displaying data, and we're going to bring them together and begin to see an actual application at work.