惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
M
MIT News - Artificial intelligence
罗磊的独立博客
博客园 - 【当耐特】
A
About on SuperTechFans
Last Week in AI
Last Week in AI
雷峰网
雷峰网
IT之家
IT之家
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
博客园 - 叶小钗
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - Franky
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Docker
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
The Cloudflare Blog
大猫的无限游戏
大猫的无限游戏
阮一峰的网络日志
阮一峰的网络日志
S
SegmentFault 最新的问题
Recent Announcements
Recent Announcements

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
React useContext + useNavigate Scenarios
Athithya Siv · 2026-05-29 · via DEV Community

Athithya Sivasankarar

1. Login Scenario

Problem

User enters name in Login page and moves to Dashboard page.
Dashboard should display the username.

App.jsx

import React, { createContext, useState } from "react";
import { BrowserRouter, Routes, Route } from "react-router-dom";

import Login from "./components/Login";
import Dashboard from "./components/Dashboard";

export const UserContext = createContext();

function App() {

  const [user, setUser] = useState("");

  return (

    <UserContext.Provider value={{ user, setUser }}>

      <BrowserRouter>

        <Routes>

          <Route path="/" element={<Login />} />

          <Route
            path="/dashboard"
            element={<Dashboard />}
          />

        </Routes>

      </BrowserRouter>

    </UserContext.Provider>

  );
}

export default App;

Enter fullscreen mode Exit fullscreen mode

Login.jsx

import React, { useState, useContext } from "react";
import { useNavigate } from "react-router-dom";
import { UserContext } from "../App";

function Login() {

  const [name, setName] = useState("");

  const { setUser } = useContext(UserContext);

  const navigate = useNavigate();

  const handleLogin = () => {
    setUser(name);
    navigate("/dashboard");
  };

  return (
    <div>

      <input
        type="text"
        placeholder="Enter Name"
        value={name}
        onChange={(e) => setName(e.target.value)}
      />

      <button onClick={handleLogin}>
        Login
      </button>

    </div>
  );
}

export default Login;

Enter fullscreen mode Exit fullscreen mode

Dashboard.jsx

import React, { useContext } from "react";

import { UserContext } from "../App";

function Dashboard() {

  const { user } = useContext(UserContext);

  return (

    <div>

      <h1>Dashboard</h1>

      <h2>Welcome {user}</h2>

    </div>

  );
}

export default Dashboard;

Enter fullscreen mode Exit fullscreen mode

2. Logout Scenario

Problem

User clicks Logout button and should return to Login page.
Stored user data should also be cleared.

App.jsx

import React, { createContext, useState } from "react";
import { BrowserRouter, Routes, Route } from "react-router-dom";

import Login from "./components/Login";
import Dashboard from "./components/Dashboard";

export const UserContext = createContext();

function App() {
  const [user, setUser] = useState("");

  return (
    <UserContext.Provider value={{ user, setUser }}>
      <BrowserRouter>
        <Routes>
          <Route path="/" element={<Login />} />
          <Route path="/dashboard" element={<Dashboard />} />
        </Routes>
      </BrowserRouter>
    </UserContext.Provider>
  );
}

export default App;

Enter fullscreen mode Exit fullscreen mode

Login.jsx

import React, { useState, useContext } from "react";
import { useNavigate } from "react-router-dom";
import { UserContext } from "../App";

function Login() {
  const [name, setName] = useState("");

  const { setUser } = useContext(UserContext);

  const navigate = useNavigate();

  const handleLogin = () => {
    setUser(name);
    navigate("/dashboard");
  };

  return (
    <div>
      <h1>Login</h1>

      <input
        type="text"
        placeholder="Enter Name"
        value={name}
        onChange={(e) => setName(e.target.value)}
      />

      <button onClick={handleLogin}>
        Login
      </button>
    </div>
  );
}

export default Login;

Enter fullscreen mode Exit fullscreen mode

Dashboard.jsx

import React, { useContext } from "react";
import { useNavigate } from "react-router-dom";
import { UserContext } from "../App";

function Dashboard() {

  const { setUser } = useContext(UserContext);

  const navigate = useNavigate();

  const handleLogout = () => {
    setUser("");
    navigate("/");
  };

  return (
    <div>

      <button onClick={handleLogout}>
        Logout
      </button>

    </div>
  );
}

export default Dashboard;

Enter fullscreen mode Exit fullscreen mode

3. Name Sharing Scenario

Problem

User enters name in Home page.
Profile page should display the same name.

App.jsx

import React, { createContext, useState } from "react";
import { BrowserRouter, Routes, Route } from "react-router-dom";

import Home from "./components/Home";
import Profile from "./components/Profile";

export const UserContext = createContext();

function App() {
  const [user, setUser] = useState("");

  return (
    <UserContext.Provider value={{ user, setUser }}>
      <BrowserRouter>
        <Routes>
          <Route path="/" element={<Home />} />
          <Route path="/profile" element={<Profile />} />
        </Routes>
      </BrowserRouter>
    </UserContext.Provider>
  );
}

export default App;

Enter fullscreen mode Exit fullscreen mode

Home.jsx

import React, { useContext } from "react";
import { UserContext } from "../App";

function Home() {

  const { user, setUser } = useContext(UserContext);

  return (
    <div>

      <input

        type="text"
        value={user}
        onChange={(e) => setUser(e.target.value)}
      />

    </div>
  );
}

export default Home;

Enter fullscreen mode Exit fullscreen mode

Profile.jsx

import React, { useContext } from "react";
import { UserContext } from "../App";

function Profile() {

  const { user } = useContext(UserContext);

  return (
    <div>
      <h1>{user}</h1>
    </div>
  );
}

export default Profile;

Enter fullscreen mode Exit fullscreen mode

4. Button Navigation Scenario

Problem

User clicks Next button in Home page and About page should open.

App.jsx

import React from "react";
import { BrowserRouter, Routes, Route } from "react-router-dom";

import Home from "./components/Home";
import About from "./components/About";

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
      </Routes>
    </BrowserRouter>
  );
}

export default App;

Enter fullscreen mode Exit fullscreen mode

Home.jsx

import React from "react";
import { useNavigate } from "react-router-dom";

function Home() {

  const navigate = useNavigate();

  return (
    <div>

      <button onClick={() => navigate("/about")}>
        Next
      </button>

    </div>
  );
}

export default Home;

Enter fullscreen mode Exit fullscreen mode

About.jsx

import React from "react";

function About() {
  return (
    <div>
      <h1>About Page</h1>
    </div>
  );
}

export default About;

Enter fullscreen mode Exit fullscreen mode

5. Back Navigation Scenario

Problem

User opens Details page and clicks Back button to return previous page.

App.jsx

import React from "react";
import { BrowserRouter, Routes, Route } from "react-router-dom";

import Home from "./components/Home";
import Details from "./components/Details";

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/details" element={<Details />} />
      </Routes>
    </BrowserRouter>
  );
}

Enter fullscreen mode Exit fullscreen mode


jsx
import React from "react";
import { useNavigate } from "react-router-dom";

function Home() {
const navigate = useNavigate();

return (


Home Page

  <button onClick={() => navigate("/details")}>
    Go To Details
  </button>
</div>

);
}

export default Home;

export default App;


jsx

Home.jsx

Details.jsx

import React from "react";
import { useNavigate } from "react-router-dom";

function Details() {

  const navigate = useNavigate();

  return (
    <div>

      <button onClick={() => navigate(-1)}>
        Back
      </button>

    </div>
  );
}

export default Details;