4IT580: Docs
4IT580 WebGitLab

2nd Practical Class:
React Context

In the previous lecture, we learned how to manage local state and encapsulate logic into custom hooks. Now we’ll extend that knowledge to sharing state across multiple components using React Context.

What is Context?

React Context is a way to share data (state, config, functions) across the component tree without prop drilling.

Creating and Using Context

Define Context

Provide Context

Consume Context

Real-World Example

We can use context for auth state:

Instead of drilling props, provide them at the top (or in a scoped section):

Scoped Context Example

We don’t need to wrap the whole app in a provider. Sometimes it’s better to keep the state scoped to a part of the UI.

Example: local NotificationsContext for a dashboard section:

✅ This shows how context can be scoped only to a part of the UI, keeping global state smaller and more efficient.

Recap

Resources