What is Context API?
Last updated
Was this helpful?
Last updated
Was this helpful?
Context provides a way to pass data through the component tree without having to pass props down manually at every level.
So If we have a parent component with child components inside it and each child component has further child components, then to pass data from the parent component to the innermost child component we have to pass it through all the parent components of the innermost child component. This is known asprop drilling
.
This means even if any component is not using that prop, it has to receive that prop to pass it down to the child component. So to avoid this long chain of passing props through each level, we can use context API provided by React.
So using Context API, the innermost child can directly access the prop data sent from the parent component just like the connect
method of react-redux
library.
That’s enough for details. Let’s start with code to understand it better.
Clone the project repository from which is created in .
To run the application, execute the following commands in sequence
1. yarn install 2. yarn start
The JSX returned from src/components/HomePage.js
looks like this
If you see, UsersList
component accepts users
and isLoading
props and Filters
component accepts handleSort
and sortOrder
prop.
If we have child components inside Filters
or UsersList
component which also needs these props, we have to pass them down from these components. So instead of passing those as props, we can access them using Context API.
You can find the initial source code .
There are 2 ways we can access the props passed from the context in React. Let’s start with the first approach.
Create a new context
folder inside src
folder and add UserContext.js
inside it with the following code:
Here, we are creating a UserContext
variable which we can use to access the data passed from the parent component
Now, in HomePage.js
, import the UserContext
we just created at the top
The React.createContext()
function call returns an object with two properties, Consumer
and Provider
which we can access directly from UserContext
variable as UserContext.Consumer
and UserContext.Provider
or using destructuring as follows
So now, change the JSX returned in HomePage
component from this code:
to this code:
The Provider
component accepts props inside value
prop where we can pass all the props that we need to access in the components mentioned in between the opening and closing tag.
So now Header
, Filters
and UsersList
component can directly access the props we have provided in value
prop.
Now, to access the sortOrder
prop inside Filters
component, we need to change the returned JSX in Filters.js
from this code:
to this code:
Here, In between the opening and closing tag of UserContext.Consumer
, we provide a function for which the props
we passed from HomePage.js
will become automatically passed.
So the props
will contain all the props passed such as users
, sortOrder
and isLoading
. We need only sortOrder
prop in Filters.js so we can use destructuring here to further simplify it as:
Now, we will access the isLoading
and users
prop inside UsersList
component
Change UsersList
component from this code:
to this code:
Note, we have removed the props passed in the component and we are accessing it inside the UserContext.Consumer
render prop.
So we are accessing the isLoading
and users
props passed from Context provider which are directly passed to the function defined in between the UserContext.Consumer
.
The pattern of defining a function in between the opening and closing tag is known as render props pattern
.
The React hooks API provides an easy way of accessing the passed props from consumer so there is no need of using render props pattern where we create a function to get the props values.
Just to recap, we pass the value prop to UserContext.Provider
as
Here, we are passing the object to the value prop with users
, sortOrder
and isLoading
properties.
So we can access these object properties by using useContext
hook as below
Here, we have provided the UserContext
we created in UserContext.js
to useContext
hook as a parameter
So to access sortOrder inside Filtes.js
, we can just use
and in UsersList.js
, we can access the users
and isLoading
prop values as
Note: You can use the
useContext
hook to access props only in functional components and not in class based components. If you are using class based components, you have to use the render props pattern.
Conclusion:
React provides an easy way to avoid prop drilling using Context API. There are two ways to access the passed props. First is using render props pattern
and second is using useContext
hook.
Note that, we have just passed users
, sortOrder
and isLoading
in the value prop. If you want you can even pass the handleSort
and handleSearch
handlers in the value prop. There is no problem in doing that. Also, If you have nested children like parent component contains child component and that child component contains another child component and the innermost child is using the state or props of parent component, then only you should use context otherwise you can just directly pass props.
Context
just provides a better way to access props directly in the innermost child without the need of passing the props through each child to reach the innermost child.
You can find complete source code until this point
You can find complete source code for useContext
hook functionality