Below is an interactive React Breadcrumb component for navigating nested objects. It uses emojis for a fun learning experience:
import React, { useState } from 'react'; const Breadcrumbs = ({ data }) => { // "path" holds the keys to navigate the nested object const [path, setPath] = useState([]); // Get current nested value using the path const currentData = path.reduce((acc, key) => acc && acc[key], data); // Navigate back to a specific breadcrumb level const handleClick = (index) => setPath(path.slice(0, index + 1)); // Drill down by adding a key to the current path const handleDrillDown = (key) => { if (currentData && typeof currentData === 'object') { setPath([...path, key]); } }; return ( <div style={{ fontFamily: "sans-serif", padding: "20px" }}> {/* Breadcrumb Navigation */} <div style={{ marginBottom: "15px", fontSize: "1.2em" }}> <span onClick={() => setPath([])} style={{ cursor: "pointer" }}> π Home </span> {path.map((key, index) => ( <span key={index}> {" "} π{" "} <span onClick={() => handleClick(index)} style={{ cursor: "pointer" }}> {key} </span> </span> ))} </div> {/* Content Display */} <div> {currentData && typeof currentData === "object" ? ( <ul style={{ listStyleType: "none", paddingLeft: 0 }}> {Object.keys(currentData).map((key) => ( <li key={key} onClick={() => handleDrillDown(key)} style={{ cursor: "pointer", margin: "5px 0" }}> π {key} </li> ))} </ul> ) : ( <div style={{ fontSize: "1.2em" }}>π {currentData !== undefined ? currentData : "No data"}</div> )} </div> </div> ); }; export default Breadcrumbs;
import React from 'react'; import ReactDOM from 'react-dom'; import Breadcrumbs from './Breadcrumbs'; const nestedData = { Fruits: { Citrus: { Orange: "Sweet and juicy π", Lemon: "Sour and zesty π" }, Berries: { Strawberry: "Red and delicious π", Blueberry: "Small and mighty π«" } }, Vegetables: { Leafy: { Spinach: "Healthy and green π₯¬", Lettuce: "Great for salads π₯" }, Root: { Carrot: "Crunchy and sweet π₯" } } }; ReactDOM.render(<Breadcrumbs data={nestedData} />, document.getElementById('root'));
This component is reusable and interactiveβperfect for learning nested object navigation while having a bit of emoji fun!
import React, { useState } from 'react'; const Breadcrumbs = ({ data }) => { // "path" holds the keys to navigate the nested object const [path, setPath] = useState([]); // Get current nested value using the path const currentData = path.reduce((acc, key) => acc && acc[key], data); // Navigate back to a specific breadcrumb level const handleClick = (index) => setPath(path.slice(0, index + 1)); // Drill down by adding a key to the current path const handleDrillDown = (key) => { if (currentData && typeof currentData === 'object') { setPath([...path, key]); } }; return ( <div className="bg-gray-100 p-5 rounded-lg max-w-md mx-auto"> {/* Breadcrumb Navigation */} <div className="mb-4 text-lg"> <span onClick={() => setPath([])} className="cursor-pointer hover:text-blue-600 transition" > π Home </span> {path.map((key, index) => ( <span key={index}> {" "} π{" "} <span onClick={() => handleClick(index)} className="cursor-pointer hover:text-blue-600 transition" > {key} </span> </span> ))} </div> {/* Content Display */} <div> {currentData && typeof currentData === "object" ? ( <ul className="list-none pl-0"> {Object.keys(currentData).map((key) => ( <li key={key} onClick={() => handleDrillDown(key)} className="cursor-pointer my-2 p-2 bg-white rounded hover:bg-blue-50 transition" > π {key} </li> ))} </ul> ) : ( <div className="text-lg font-semibold"> π {currentData !== undefined ? currentData : "No data"} </div> )} </div> </div> ); }; // Sample nested data structure const sampleData = { "User Profile": { "Personal Info": { "Name": "John Doe", "Age": 30, "Email": "john.doe@example.com" }, "Address": { "Street": "123 Main St", "City": "Anytown", "Country": "USA" } }, "Work Details": { "Company": "Tech Innovations Inc.", "Position": "Senior Developer", "Department": { "Name": "Engineering", "Team": "Web Development" } }, "Hobbies": { "Sports": ["Hiking", "Cycling"], "Music": "Playing Guitar", "Reading": ["Science Fiction", "Technology Books"] } }; // App component to demonstrate the Breadcrumbs const App = () => { return ( <div className="p-6"> <h1 className="text-2xl font-bold mb-4">Interactive Nested Data Explorer</h1> <Breadcrumbs data={sampleData} /> </div> ); }; export default App;
Hereβs a mnemonic + visual metaphor to help you remember how to implement a breadcrumb component for nested objects:
B β Break the object using a path array (e.g., ['Home', 'Products'])
R β Render the path as clickable segments
E β Extract children from the nested object using reduce
A β Allow navigation by slicing the path
D β Drill-down by appending keys to the path on click
Think of the object like a folder tree , and the breadcrumb is just a GPS trail β every time you go deeper, you're adding a breadcrumb to your trail. Clicking a breadcrumb takes you back to a previous node in the tree.
Object Tree ββ Path Array ββ Breadcrumb UI --------------------------------------------------------------- data ββ ['Home', 'Products'] ββ Home / Products β β drill using reduce build from array
Object + Path[] + Reduce + Render path as links
Thatβs all a breadcrumb for nested objects really is.
Want me to generate a flashcard version or a diagram to make it stick even harder?
Test your understanding with 3 quick questions