With all the concepts we have learned in this section, you are more than ready to create your own React applications. However, there are a lot of other, more advanced concepts out there that we haven’t discussed, but need to inform you of.
This lesson will be used to introduce you to a few advanced concepts and give you some tips on your path as a React developer. This section isn’t meant to teach these concepts, but to give you a high-level overview to enable you to research in the event you need them or wish to learn more.
This lesson is meant as a checklist. Once you are secure with the basics, come back to this lesson and work through the advanced concepts. This doesn’t have to be now. If you first want to move ahead in the curriculum and apply your knowledge or feel like you do not need a particular concept right now, just skip it and come back another time when you are ready.
As a friendly reminder, Google and StackOverflow are your friends! We should be familiar with these tools at our disposal at this point.
By the end of this lesson, you should be able to do the following:
- Explain PropTypes.
- Explain Styled Components.
- Explain Redux and state management systems.
- Explain how Context API works.
- Explain higher-order Components.
- Explain how to make your own React Hooks.
Guide to Advanced React
2. Styled Components
If you start writing larger applications you will, with certainty, encounter the problem that you might want to style buttons or anything else across your application similarly. One way you could solve this problem is through code duplication (writing even the most basic CSS for each button). However, this wouldn’t be a very good (or clean) code. The styled-components package provides a cleaner way to do this. It allows you to give some default stylings to HTML elements, meaning you define a button with some basic styling and reuse this button throughout your application. This way there is no code duplication and it allows your application to be more scalable.
You might have already heard about Redux. Redux is the most popular state management system out there. It is not a part of React, but the two can be very easily combined. Together, they make up an extremely powerful duo. The purpose is to store your application’s state in a single place, commonly called a “store”. You then dispatch actions to the store, where a reducer will handle the state changes. The primary benefit of using a state management library is to prevent having to pass props through multiple levels of the component tree. A state management library is often only recommended for larger applications.
4. Context API
As your application becomes larger and you have multiple components to ensure reusability, you might find yourself passing props through a lot of components in the middle or you might have a lot of components that require the same props. This pattern is known as
prop drilling. To help avoid this, React provides the Context API that lets a parent component provide data to all the components in its tree without having to pass props. Imagine that you have implemented an optional dark theme in your website and quite a few of your components require this theme data to correctly style itself - having a
context of your theme will allow all children components to have access to this data.
The Passing Data Deeply with Context webpage on React Docs Beta does an excellent job of explaining on how to use the Context API. Furthermore, if you wish to learn more advanced concepts in React, React Docs Beta is a great resource to dig into.
5. Higher-order Components
Higher-order components are components that consume another component and return a third component. This article on HOC’s by Smashing Magazine shows different use cases and provides some great comparisons to help you understand.
6. More Hooks
There are a lot of hooks out there, with the number only increasing by the day (since you can write your own!). As you have seen in previous lessons, it seems like the React team wants us to use more functional components with hooks in the future, so it is definitely worth getting to know both the hooks that are built-in to React, as well as how to create your own.
This section contains questions for you to check your understanding of this lesson. If you’re having trouble answering the questions below on your own, review the material above to find the answer.
- What are PropTypes and why can it be beneficial to use them?
- What are Styled Components and how do they make your code cleaner?
- What is Redux and why are state management systems used in many large applications?
- What is prop drilling and how can you use Context API to avoid it?
- What is a High-order Component?
- How can you create your own Hooks?