Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
React redux 3
  • Overview
  • Transcript

4.2 Using the `react-redux` Package

It’s a best practice in Redux applications to make a distinction between presentational components and container components. In this lesson, I’ll explain what these are and how we can use them.

Related Links

4.2 Using the `react-redux` Package

So far we've gotten a small taste of what it's like to build applications in React and Redux. However you might notice something, almost all of our components need to work with the store either by reading specific stage or by dispatching actions back to the store. This means that most if not all of our components need to be able to access the store in some way. You know right now we just have this global store object that we can access directly here where we are assigning properties to our sidebar. However having a global store like this is not great for a lot of reasons. One of the simplest reasons is that since it's not actually global it's only accessible from within this file, that means that we would have to use it from within this file, or we have to start passing it around from this file to other files and that's just going to become a total mess. Also Also, if we have a lot of nested components, it means that if an in-between component doesn't really need the store but one of its children does, we're going to have to pass it to that in-between component anyway. Because it needs it so that it can pass it to its child. This is probably sounding a little bit confusing, and the truth is, it can get confusing. Now, it would be nice if there was some way we could give all of our components access to the store without having a global variable and without actually manually passing it around, and the truth is, there is a way. We can actually do this manually, with the react idea of context, and we're not going to dive into context in this course you could consider it one of the advanced features in react, but instead we're going to use another package and this package is called react redux. Now you can read a little bit more about the ideas behind this package. If you head over to the Redux documentation, there's a page here on usage with react, and it talks about how you can use this React Redux package. And one of the main ideas between this package is the idea of presentational components and container components. Basically, we can break our application up into two sets of components. One are the presentational components, as you can see here, these are concerned with how things look, they don't have to be aware of Redux at all. They just read data from their properties and they can change data by invoking callbacks that we also assign as properties, containers on the other hand, are aware of Redux. And they specifically subscribe to Redux state and dispatch Redux actions. And we can create a container component by simply wrapping a presentational component with some of these instructions. So if you want to find out more about the details here go ahead and read this document. There's also a great link here to an article that Dan Abramov, author of "Redux" has written about presentational and container components, so you can read all about the thinking behind those in those articles, but for now let's go ahead and dive in and actually see how this can work. And for now we're going to use the sidebar as an example of a component that we can split into presentational and container components. Now you might be a bit confused here how we're gonna split our sidebar into two separate components, but the truth is container components are always going to be wrapping presentational components. In fact often you may have a presentational component that has only one job, and that is to be wrapped by one specific container component, and that's exactly what we're going to do here with sidebar Later on we start creating models for creating editing cards. You'll see how we can create a single presentational component that can actually be wrapped by two separate container components, and give us some different actions. So, let's give this a try. Of course we'll have to start by installing the reactor ducts library. So, let's do N.P.M. install dash, dash, save, react, dash, redux this will just take a minute to install. Okay, so now that that has installed we can go ahead and import it here. Now in our main file here app dot J.S. we actually only need the provider component given to us by react dash redux. Now the provider component is actually the part of reactor docs that's going to take our store and pass it around to these different components. Actually what happens is behind the scenes, provider is using the context feature that I mentioned. So if you have a little bit more advanced reactor experience and you played around with the context before, then that might give you an insight into how exactly the provider is working. The provider actually makes it really really simple to use the store everywhere. All we have to do is wrap our top level application component here in a provider component. So we can call provider here and we give it the store as one of its properties. And then, of course, I'll add the closing provider tag after the app closes and now our application is using the react redux provider. And, of course, if I go back to our application now, you can see that nothing has actually changed. However our provider is wrapping all of our components and giving them the store as context. Now what we could do is inside of our sidebar component access the context directly. And from that context we could get the store, which means we would be able to get our properties here for decks and adding decks. And we could also get all of our property functions at deck show at deck and hide at deck. However, we're not gonna do it quite like that. Let's open up a sidebar file, and let's import something here is well. Let's import the connect function from react-redux. What the connect function does is it allows us to define exactly what properties and functions we want a container component to have. And then we can take that definition of functions apply it to a presentational component and get a complete react component. Now, I understand that sounds a little bit confusing. So, let's see how this is done. The beauty of the sidebar that we've already written here is that it's actually already a presentational component. And we can actually see this if we go back to the usage with react page in the Redux documentation, and look at this table here, which defines presentational components. First of all, our side bar's only really concerned with how things looked. It gives us markup and styling of course it's not aware of Redux at all, if you look through this you'll see that there's no knowledge of Redux at all. We do have these methods that we call show add deck and add deck and hide add deck, but those methods know about Redux. Sidebar itself doesn't know anything at all about redux. In fact if we wanted to take redux out of this project and use some alternative. We can just change the definitions of these functions and this side of our component doesn't have to change at all. It just calls these functions so as you can see this component is not aware of redux. How does it read data? Well it just reads data from properties that we've given it, right? If we go back to our apt dot J.S. file here you can see we've given it the decks properties, and the adding deck property and that's the data that it reads from the state. How about for changing data? Well, it just invokes callbacks that are from properties. We have these three methods, and when it invokes those, the data is changed in the store. And finally, of course, yes, it is written by hand, because we wrote this out manually. And as you'll see in a second, container components will be generated by React Redux. So, we already have one of the two pieces we need and that is this side bar as a presentational component. The next thing we want to do is take these property definitions that we're giving to sidebar, and instead of defining them here let's define them as part of our container component. So I'm just going to go ahead and copy these lines and I'm going to paste them at the top here. Now as we just saw in the Redux documentation, these properties are actually nicely split into two sets. The properties that are data and the properties that are functions to call which perform actions that change the store. So what we need to do now is create two functions that will map the state to these properties. Now traditionally within react, these functions are called mapStateToProps, and the other one is called Map dispatch to props. So, let's go ahead and start with ma state to props. Now this is a function that will receive the latest state from the store. So, it doesn't receive the store itself instead it receives the state, and now in here we just need to return an object and this object is going to have two properties, and as we already saw up here they are the decks and adding decks property. So I can actually just copy these and paste them down here, because this is practically the same data. We just need to convert the syntax to be object literal syntax instead of J S X syntax. So this is our mapStateToProps function, basically it just takes the current state from the store and don't worry, you'll see where we get this state in a second, and it returns whatever data or presentational component will need. So it needs the decks and the addingDeck property and so we return those within an object, and we can actually do a few things to clean this up a little bit. First of all we could actually get rid of these curly braces that are the block for this function, because we only have one statement here that we're returning. But then because we only have one statement here that were returning. But then, because we only have that one line, we can get rid of the return statement. However now, we have curly braces here and JavaScript is going to think this is the function block, so we'll wrap those in parentheses. And now, we're still just returning that object. However, we can shorten this up a little bit more, because we don't need the whole state object, so let's use the destructuring syntax, and say we just want the decks property, and the adding deck property from this object. Of course then inside this function we don't say state decks. We just say decks and we don't say state adding deck. We just say adding deck and now I think you can see where we're going with this, because the key and the property have the same name. We can get rid of one of those and we can just say decks and addingDeck, and this is the shorter version of our function thanks to ESX. So now what about mapDispatchToProps? Well, this is a function as well and it's going to take dispatch as its only parameter. Now dispatch of course is the stored.dispatch function. Once again we're just going to return an object literal, so don't forget those parentheses, and in here we need these three properties that we have up here, and again I can copy these and paste them down here, because they're gonna be so similar. So let's see, we have addDeck, which is our function. It's going to take a name and it's going to call dispatch and we're going to run the addDeck function. All right, and we can't forget we're going to have to pull in those actions. So we'll get that soon. Next we have showAddDeck which takes no properties and calls dispatch with showAddDeck. Finally, we have hideAddDeck, which will act in the same way. Okay, and now we have a function that maps the dispatch function to the three callbacks that we need for a sidebar. Okay, let me get rid of this comment, and now we're going to need to get those actions. So I will copy this import here from after yes and I'll just paste it in here. Excellent, okay, so now we have everything we need to create our container component. We have two functions that will map our state object and our dispatch function to the properties that this function needs. And we have a presentational component that expects these properties. All right, and now I think you know which function we're gonna need to use because we imported connect here from react-redux, but we haven't used it yet. Connect is what we can use to connect these two mapping functions with our presentational component. And what this connect function will return is our container component. And this is why the documentation says the container components are usually generated by react-redux. We're not actually going to write another component here. Instead we pass these three pieces to the connect function and it is going to return our container component. So the way this works is down here at the bottom, instead of exporting sidebar. Let's export a call to connect and we're going to pass it two parameters, that's right just two. We're going to pass it map state to props and we're going to pass at map dispatch to props and this is going to return a new function. So let's go ahead and call that new function right away. So we do have two sets of parentheses up against each other here. That's perfectly acceptable and in here we just pass in our presentational component, so sidebar. All right so now what's actually exported from this file is not a presentational side bar, but instead it's our new container component which outside of this function we could still refer to a sidebar. So let's head over to the browser and see if things are working and of course. We can't find the module actions and of course that makes perfect sense, because when I copied a line four here, which imports these actions, we moved into the components directory. So we need to say dot dot actions instead of just dot actions. Now if we come back to our page here and give our workflow a try, you can see that once again it's working just fine, that's excellent. Now we're not quite done yet, because if we come back to the app JS file here, there's still a little bit of extra stuff. First of all We're still importing our sidebar from our sidebar file but it's not really the same sidebar we're using before. Instead it's a sidebar that already knows what it needs. And so down here in our run function we can actually remove all of these properties from our sidebar. And we can just call our sidebar Just like that, just say get the sidebar. We don't actually have to give it any properties. This means of course that we no longer need these actions from up here. So I can delete the actions import as well. All right, so that cleans up our app.js file a little bit more. And if we come back to the browser, it's already refreshed and you can see that our workflow still works. Excellent so that is the React Redux package, and we're going to be using this for all of our components as we go from here on.

Back to the top