Load data into another component in ContextAPI

The Context API solves one major problem–prop drilling.ContextAPI or Redux can be used for PropDrilling problem. Load data into another component in ContextAPI.

Context provides a way to pass data through the component tree without having to pass props down manually at every level.

And sometimes, some components would have to receive the props only to pass it down to its children. This is known as Prop Drilling. 

In Below Image, you can see I have created one Button and table. On Button click the table loads with API Data. But Button is Components A and Table is in Components B . So Here we do use ContextAPI or Redux not Prop Drilling.

CRUD Operation in ContextAPI

Now you can see our Output image and Now below is the folder structure

Folder Structure of the Project

ContextAPI Folder Structure

First, make a context.js file that will be used for context support. Without this file, we cant use the context API concept in our project


import { createContext } from 'react';
export const AppContext = createContext();

Now I have created Header.js for menu


import React from 'react'

function Header() {
    return (
            <nav class="navbar navbar-dark bg-primary">
            <a class="navbar-brand" href="#" style={{color:"white"}}>Navbar</a>

export default Header

Now We will See each component by code. So first we will see sidebar ( Load Button Component)


import React, { useContext, useState } from 'react';
import { AppContext } from '../context';

const AddUser = () => {
	const { dispatchUserEvent } = useContext(AppContext);
    const [showdata ,setShowData] = useState([])

	const handleAddUser = (e) => {
        .then(response => response.json())
        .then(data => setShowData(data))
		dispatchUserEvent('ADD_USER', { newUser: {showdata} });

	return (
		<div  style={{background:"#f2f2f2",height:"500px",marginTop:"-20px"}}>
			<h3 className='text-success' style={{marginTop:"30px",position:"relative",textAlign:"center"}}>Load data</h3>
			<button className='btn btn-primary' onClick={handleAddUser}>Load User</button>

			<h6 className='text-success' style={{marginTop:"30px",position:"relative",textAlign:"center"}}>This Button is in Component  A</h6>

export default AddUser;

Now we will see table Component ( Left Content table )


import React, { useContext } from 'react';
import { AppContext } from '../context';

const UserList = () => {
	const  {users} = useContext(AppContext);
	return (
		<div style={{background:"#ececec",height:"500px",marginTop:"-20px"}}>
			<h3 className='text-success' style={{marginTop:"30px",position:"relative",textAlign:"center"}}>Get Data from Another components in React JS</h3>
		    <h6 className='text-danger' style={{marginTop:"03px",position:"relative",textAlign:"center"}}>This Table is in Component Folder B</h6>
            <div style={{padding:"20px"}}>
			 {users.map((user) =>
                      <table class="table">

export default UserList;

Now I will call both Components ( Button and Table Component ) in the LoadData.js file

import React, { useState } from 'react';
import './App.css';
import AddUser from './componentsA/AddUser';
import UserList from './componentsB/UserList';
import { AppContext } from './context';
import  Header from './Header';

function LoadData() {
	const [ users, setUsers ] = useState([]);
	const dispatchUserEvent = (actionType, payload) => {
		switch (actionType) {
			case 'ADD_USER':


	return (
		<div className="App">
            <div className='container-fluid'>
                <div className='row'>
                    <AppContext.Provider value={{ users, dispatchUserEvent }}>
                       <div className='col-md-3'>
                        <AddUser />
                       <div className='col-md-9'>
                          <UserList />

export default LoadData;

Just call the LoadData.js file in the main file ( App.js )


import './App.css';
import LoadData from './LoadData';

function App() {
  return (
    <div className="App">

export default App;

Now run the code using npm start. So It will run on http://localhost:3000/

Subscribe my Youtube Channel Before Downloading the code


Source Code : Download the Source Code

See our More Project in Laravel and ReactJS

Theme Color change in React JS

Records display in Material UI Table

Display Records in Model Box in React

Responsive Navbar in Material UI React

Upload PDF file in React, Laravel

Comments 1

Leave a Reply

Your email address will not be published. Required fields are marked *