From the course: React: Software Architecture

Unlock the full course today

Join today to access over 24,500 courses taught by industry experts.

Accessing context inside components

Accessing context inside components

From the course: React: Software Architecture

Accessing context inside components

- [Instructor] So at this point, we're providing the context to any component inside of this provider that wants it. So what we're going to do is open up our CounterButton component now and allow it to access the value of that context. So here's what that's going to look like. We're going to start off by saying import useContext from react, and we're going to import our CounterContext. We'll say import CounterContext from CounterContext. All right, and now we're going to use this useContext hook in conjunction with our CounterContext to get the current value of this useState hook inside our CounterProvider. So here's what that'll look like. We're going to say const, and we're going to use object destructing here to get both the numberOfClicks and the increment function. And we'll say equals useContext and pass the CounterContext as an argument. Okay, now we'll get to this incrementBy thing in a second. Let's just comment…

Contents