From the course: React: State Management (2019)

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Using the Context API

Using the Context API - React.js Tutorial

From the course: React: State Management (2019)

Start my 1-month free trial

Using the Context API

- [Narrator] The concept of context is most easily introduced with an example. To do this we're going to create a folder called context where we'll define our book context file. Lets go ahead and do that now. Cool, so lets go line by line and implement this. It's a pretty short file. First, we're going to import react, and then we're going to define our context. Now, notice that we're creating context with an empty object, that's totally okay, it's going to get populated with context from our provider component later on. The other thing I'm going to do here is define a provider, and a consumer. What this does is, that it's a nice substraction and it makes it easier to read code. It's not a necessity, it's just something nice to do. So, next we'll insert the provider and consumer. To show the power of context, we'll introduce a button called toggle authors, which decides whether BookItem displays the author or not. Notice that BookList won't need this information but BookItem will…

Contents