Suppose a student writes a note, opens a Help tab, then returns to Notes. A normal conditional render can remove the notes component and lose its local state. React's <Activity> component can hide the view and restore its state when it becomes visible again. This small learning desk lets you see that behavior yourself.
Notes: type a sentence → Help: read a tip → Notes: sentence remains
The Notes component stays in the Activity boundary while its view is hidden.
What does Activity do?
An Activity boundary has a mode of visible or hidden. Hidden content is visually hidden, and React cleans up its Effects. When it becomes visible again, React restores the component's internal state and starts its Effects again. The example needs React 19.2 or newer, including React 19.3.
Set up the example
In a new React project, run npm create vite@latest student-activity -- --template react, cd student-activity, and npm install. Check that your project has React 19.2 or newer. Replace src/App.jsx with this complete example, then run npm run dev:
import { Activity, useState } from 'react';
function StudentNotes() {
const [note, setNote] = useState('');
return (
<section>
<h2>Student notes</h2>
<label htmlFor="note">What did you learn today?</label>
<textarea
id="note"
value={note}
onChange={(event) => setNote(event.target.value)}
rows={4}
/>
<p>Characters written: {note.length}</p>
</section>
);
}
function StudyHelp() {
return (
<section>
<h2>Study help</h2>
<p>Write one concept in your own words, then try a small example.</p>
</section>
);
}
export default function App() {
const [tab, setTab] = useState('notes');
return (
<main>
<h1>Student learning desk</h1>
<nav aria-label="Learning desk tabs">
<button type="button" onClick={() => setTab('notes')}>
Notes
</button>
<button type="button" onClick={() => setTab('help')}>
Help
</button>
</nav>
<Activity mode={tab === 'notes' ? 'visible' : 'hidden'}>
<StudentNotes />
</Activity>
<Activity mode={tab === 'help' ? 'visible' : 'hidden'}>
<StudyHelp />
</Activity>
</main>
);
}
Type something in the Notes field, choose Help, then return to Notes. Your text and character count should still be there. Reloading the entire browser page starts a new session for this example, so the note will reset.
Why does the note stay?
StudentNotes owns the note state. The Activity boundary stays in the component tree while its mode changes. If you instead rendered StudentNotes only when tab === 'notes', switching away would unmount it and its local state would be lost. Activity is useful for tabs or panels that users are likely to revisit.
When should you use it?
Use Activity when preserving a hidden view's state makes the experience better. It does not save data permanently; for a real student note, send it to a server or store it appropriately. Hidden content can still use memory, so avoid keeping many large views mounted without a reason. If a simple conditional render is enough, keep the simpler approach.
Practice
- Add a third tab named Checklist with two checkboxes. Switch away and back to see whether their state remains.
- Replace the Notes Activity boundary with conditional rendering and compare what happens to the typed note.
- Add a Save button and explain what storage you would use for notes that must survive a page reload.
Continue with the React learning path. See the official Activity reference and React 19.2 release notes.
No comments:
Post a Comment
Please do not enter any spam link in the comment box.