DEV Community

Cover image for What is Context API in React and how to use it?
Ashutosh Dash
Ashutosh Dash

Posted on

What is Context API in React and how to use it?

N.B. This article requires you to have little knowledge of prop drilling and the issue associated with them.

Context API acts as a container for the global state variables which is helpful when you want to avoid prop drilling and pass data from one component to another without the need for passing props.

Steps to use Context API

Step 1: Create Context

create context

Step 2: Use Context

use context

And that's it. You have successfully implemented Context API.

Overall Code

Overall Code

Final Output:

final output

Thanks for reading this article. I hope it was helpful. Feel free to let me know how I could improve it.
You can connect with me on LinkedIn and Twitter.

Tiugo image

Modular, Fast, and Built for Developers

CKEditor 5 gives you full control over your editing experience. A modular architecture means you get high performance, fewer re-renders and a setup that scales with your needs.

Start now

Top comments (0)

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay