Fetching Notes from Context in Our CloudNoteBook App ๐๐
Until now, every component that needed note data had to receive it through props,
passed down from parent to child. But as our CloudNoteBook App grows, passing props
through multiple levels (called prop drilling) becomes messy and hard to maintain.
This is exactly the problem React's Context API was built to solve โ and in this
React JS tutorial for beginners, we'll use it to fetch and share notes across our app
without passing a single prop.
In this tutorial, we will learn:
NoteContext with createContextNoteProvider to hold and fetch note datauseContext hook to consume notes in any componentWhat is the React Context API?
The Context API is React's built-in way to share data โ like notes, user info, or
theme settings โ across many components without manually passing props at every level.
In CloudNoteBook, instead of each component fetching its own notes, we'll fetch them
once inside a NoteProvider and let any component anywhere in the tree
read them instantly using useContext.
Step 1: Create the NoteContext
First, create a new file context/notes/NoteContext.js. This defines the context
object our whole app will use to access notes.
import { createContext } from "react";
const NoteContext = createContext(null);
export default NoteContext;
Step 2: Build the NoteProvider
Next, create context/notes/NoteProvider.js. This component holds our
notes and loading state, fetches the notes, and makes them
available to every component wrapped inside it.
import React, { useState, useEffect } from "react";
import NoteContext from "./NoteContext";
const NoteProvider = (props) => {
const [notes, setNotes] = useState([]);
const [loading, setLoading] = useState(true);
const fetchNotes = () => {
setLoading(true);
// Simulated fetch โ will be replaced by a real CloudNoteBook API call
setTimeout(() => {
setNotes([
{ id: 1, title: "Grocery List", content: "Milk, Eggs, Bread" },
{ id: 2, title: "React Context Notes", content: "useContext avoids prop drilling" },
{ id: 3, title: "Weekend Plan", content: "Finish CloudNoteBook tutorial" },
]);
setLoading(false);
}, 1000);
};
useEffect(() => {
fetchNotes();
}, []);
return (
<NoteContext.Provider value={{ notes, loading, fetchNotes }}>
{props.children}
</NoteContext.Provider>
);
};
export default NoteProvider;
Step 3: Wrap the App with NoteProvider
Open App.js and wrap your app's routes or components inside
NoteProvider, so every nested component can access notes from context.
import React from "react";
import NoteProvider from "./context/notes/NoteProvider";
import Notes from "./Notes";
function App() {
return (
<NoteProvider>
<div className="container my-4">
<h2 className="text-center mb-4">CloudNoteBook ๐</h2>
<Notes />
</div>
</NoteProvider>
);
}
export default App;
Step 4: Fetch Notes with useContext
Now create Notes.js. Instead of receiving notes as props, this component pulls
them directly from NoteContext using the useContext hook โ
no prop drilling required.
import React, { useContext } from "react";
import NoteContext from "./context/notes/NoteContext";
const Notes = () => {
const { notes, loading } = useContext(NoteContext);
if (loading) {
return <h5 className="text-center my-4">Loading your notes...</h5>;
}
return (
<div className="row">
{notes.map((note) => (
<div className="col-md-4 mb-3" key={note.id}>
<div className="card shadow-sm">
<div className="card-body">
<h5 className="card-title">{note.title}</h5>
<p className="card-text">{note.content}</p>
</div>
</div>
</div>
))}
</div>
);
};
export default Notes;
Step 5: Full Folder Structure Recap
Here's how the Context files fit together inside the CloudNoteBook project.
src/
โโโ context/
โ โโโ notes/
โ โโโ NoteContext.js
โ โโโ NoteProvider.js
โโโ Notes.js
โโโ App.js
How Context Values Flow in CloudNoteBook
| Context Value | Purpose | Used In |
|---|---|---|
notes |
Holds the array of fetched notes shared across the app | Any component calling useContext(NoteContext) |
loading |
Tells components whether notes are still being fetched | Notes.js to show a loading message |
fetchNotes |
Allows any component to re-trigger fetching notes from context | Future "refresh" or "add note" features |
Features and Learnings:-
NoteContext using createContext.NoteProvider that fetches and holds notes in shared state.NoteProvider for global access.useContext hook to fetch notes directly inside Notes.js.