- Overview
- Transcript
3.1 Build a Pure Component
Now that we have some Redux architecture in place, we can build our first React component. It will be a pure component, also called a functional component.
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.1 Build a Pure Component
In the previous lessons, we've set up the first version of our Redux store, which is going to be managing our state as we move forward in building this application. So the next step is to actually begin building React components, which will display that state to our users. Now, many of the tutorials on React that you may have seen use React.createClass or the Essex class syntax with React components to actually create our reacts components. However, React components can also be functional components or pure components. These are pure functions that just take a set of properties and they return some JSX that defines our component. Now, I'm going to assume that you're kind of familiar with how JSX works. Really, it's similar enough to HTML that if you haven't seen it before this, working through it in these lessons should be enough to get you familiar with it. Now remember, we're writing JSX here, but remember that we have Babelify running within our Watchify command, and so Babel will be converting our JSX syntax into the react function calls. So let's go ahead and write our very first component. Right now in our project file, we have, at the top here, we have our cards reducer, than we create our store, for now, I'm gonna get rid of this stuff at the bottom, where we subscribe to this store and dispatch some events. We'll come back to that a little bit later. For now, let's create our first component. And so, let's create a constant here and this component will be called App. As I mentioned, often you'll create reactor components, and we will be doing this later on, by doing React.createClass, but what React also allows us to do is to just create a basic function like this, doesn't have to be anything special, just a regular function, and as a parameter, it will take the properties that are assigned to our App component. And then, what we can return from this function is some kind of JSX. So for example, we could return a div, just like this. Now this App component is going to be a basic wrapper for our application. Let's give it a className of app, and for now, let's make this really basic. In here, we'll just put h1, and we'll say, Hello React, just so we can actually see this in action. So, let's do that, and now we have to actually render it. So, we'll say ReactDOM, which is the ReactDOM library that we're pulling in, and we'll say ReactDOM.render, and we want to render App. So, we'll actually create an instance of our app component like this, and then we'll tell ReactDOM where to render this, so we'll render it inside that element. What did we call that? Let's open up our index.html file. We have a div with an ID of root. Okay, so I can just do, document.getElementByID('root'). Excellent. So now, if we head back to the browser, you can see that live-server has already refreshed the page and we have our element showing up on the page. If we open up the console here and look inside of our root div, you can see that we have a div with a class of app, and we have an h1 element inside of it. So this is our JSX syntax being rendered. In fact, if we look at the bundle.js file here, notice that if we scroll down here, you can see that where we're creating our app here, we actually have this converted to a React.createElement call, creating a div, giving a className of app, and then inside of that, we have another createElement call, where h1 is the element, and then ultimately, we have React.createElement for our app component. So you can see that this is one of the things Babelify is doing for us, it's converting our JSX into actual React function calls. Now because our app component here is just meant to be a wrapper for a bunch of our other components, what we actually want to do is allow this component to have children of its own. So, here what we're rendering, instead of making this a self-closing tag, we could render this and say, Hello React, inside of our app component. However, for this to render properly, we need to have our app component render its children. Now, children is just received as one of the attributes of the props object. So let me just replace the content of this div, and we will say props.children. So this, here, will just be replaced by whatever we put inside the component we're creating, and if we come back to our page, you can see that this is no longer an h1 because we've just given it that raw text. In fact, what I could do is, and let me just wrap the word React in a strong tag, and now if we go back to our page, you can see that React is bold. All right, so this is our very first component. Now, you may be thinking that we're moving a little bit too slowly here. But trust me, our application is going to get pretty complicated. So I want to make sure that we're all on the same page every step along the way. We're gonna be creating a lot more components soon, and of course, many of them will be using our state. In the next lesson, we're going to create a normal React component, instead of a pure component, as we have here.