javascript
43 lines · 7 steps
Archiving with router.refresh in Next.js
A client component archives a project, then refreshes server data without a full page reload.
Explained by
highlit
1'use client';
2
3import { useRouter } from 'next/navigation';
4import Link from 'next/link';
5import { useState, useTransition } from 'react';
6
7export default function ProjectRow({ project }) {
8 const router = useRouter();
9 const [isPending, startTransition] = useTransition();
10 const [archiving, setArchiving] = useState(false);
11
12 async function archive() {
13 setArchiving(true);
14 try {
15 const res = await fetch(`/api/projects/${project.id}/archive`, {
16 method: 'POST',
17 });
18 if (!res.ok) throw new Error('Failed to archive project');
19
20 startTransition(() => {
21 router.refresh();
22 });
23 } finally {
24 setArchiving(false);
25 }
26 }
27
28 return (
29 <li className="project-row">
30 <Link
31 href={`/projects/${project.id}`}
32 prefetch={false}
33 onMouseEnter={() => router.prefetch(`/projects/${project.id}`)}
34 >
35 {project.name}
36 </Link>
37
38 <button onClick={archive} disabled={archiving || isPending}>
39 {archiving ? 'Archiving\u2026' : 'Archive'}
40 </button>
41 </li>
42 );
43}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1router.refresh re-fetches server components in place, so a mutation's effects appear without a full navigation.
- 2Wrapping the refresh in startTransition keeps the UI responsive and exposes an isPending flag for disabling controls.
- 3Deferring prefetch to onMouseEnter avoids eager network work while still making navigation feel instant on intent.
Related explainers
javascript
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
Intermediate
8 steps
javascript
import { useState, useEffect, useCallback, useRef } from 'react'; const cache = new Map(); const inflight = new Map();
Building a stale-while-revalidate hook in React
caching
request-deduplication
custom-hooks
Advanced
10 steps
javascript
import { useEffect, useRef, useState } from 'react'; export function useDelayedFlag(active, delay = 300) { const [visible, setVisible] = useState(false);
Delaying a loading spinner with a React hook
custom-hooks
debouncing
cleanup
Intermediate
8 steps
javascript
const SWIPE_THRESHOLD = 80; const MAX_TRANSLATE = 120; export function attachSwipeToDismiss(element, onDismiss) {
Building a swipe-to-dismiss gesture in JS
touch-events
gesture-detection
dom-manipulation
Intermediate
10 steps
javascript
const { pool } = require('./db'); function withTransaction() { return async (req, res, next) => {
A per-request transaction middleware in Express
middleware
database-transactions
connection-pooling
Advanced
7 steps
javascript
function collapseConsecutiveLogs(lines, { keyFn = (l) => l.message } = {}) { const groups = []; for (const line of lines) {
Collapsing consecutive log lines in JavaScript
grouping
run-length-encoding
data-transformation
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/archiving-with-router-refresh-in-next-js-explained-javascript-8440/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.