stack-nav is a navigation library for React apps that don't use URLs. It has no runtime dependencies and manages the active screen, that screen's params, and the screens the user visited.
stack-nav is not a URL router. It covers apps where a URL router adds work without adding anything:
- Tauri and Electron windows, where there is no address bar and other router libraries are bloated/overkill.
- Small tools with a handful of screens: a wizard, a settings flow, an import dialog.
- Prototypes that would otherwise hold the current screen in
useStateand render it with a switch.
If the app needs links users can share or type, use a URL router. stack-nav keeps the stack in memory and mirrors it into window.history.state; screens never get a path.
bun add stack-nav
# or
npm install stack-navReact 18 or 19 is the only peer dependency. The ESM build is 1.2 kB gzipped.
import { createStackRouter } from "stack-nav";
type Routes = {
home: undefined;
profile: { id: number };
};
export const useNav = createStackRouter<Routes>("home");Routes maps each screen name to its params. Use undefined for screens without params. An interface Routes works too.
import { RouterView } from "stack-nav";
import { useNav } from "./nav";
const Home = () => {
const { navigate } = useNav();
return (
<button onClick={() => navigate("profile", { id: 1 })}>Open profile</button>
);
};
const Profile = () => {
const { params, goBack } = useNav("profile");
return (
<>
<h1>Profile</h1>
<p>User ID: {params.id}</p>
<button onClick={goBack}>Back</button>
</>
);
};
export const App = () => {
const { currentScreen } = useNav();
return (
<RouterView<Routes>
currentScreen={currentScreen}
screens={{ home: Home, profile: Profile }}
fallback={<p>Screen not found</p>}
/>
);
};RouterView mounts the matching screen as a component, so each screen keeps its own hooks and state. Screens receive no props; they read their params with useNav("profile"). Unknown screen names render fallback.
navigatepushes the current screen and moves to the next one.replaceswaps the current screen without growing the stack. Previous screens stay reachable through back, and screens ahead stay reachable through forward.resetclears the stack and starts again at the given screen. It replaces the current browser entry and marks the older ones stale, so back and forward skip them and return to the current stack.goBackuses the browser's back button. At the first screen it callsonRootBackwhen provided, which lets a desktop shell close its window.- Browser back and forward move through the stack, params included. They do not push new entries.
- Entries written by other code are ignored and reclaimed, so a foreign
history.statecannot change the router. - Reloading the page restores the stack from
history.state. - On the server, the router renders the initial screen. Hydration then syncs to the stored stack.
Builds the store and returns the useNav hook. Call it once, at module level. Use one router per window: every instance listens to popstate and writes the same history.state, so a second router would overwrite the first.
options:
| Option | What it does |
|---|---|
onRootBack |
Called by goBack when the stack is already at its first screen. Default: delegate to the browser. |
Returns:
| Member | What it does |
|---|---|
currentScreen |
Active screen, typed as keyof Routes. |
params |
Params of the screen passed to useNav. Pass the screen name to get a precise type. |
history |
Screens visited before the current one, newest last. Readonly. |
navigate(screen, params?) |
Pushes the current screen and moves to screen. params is required when the screen declares them. |
replace(screen, params?) |
Replaces the current screen without growing the stack. params is required when the screen declares them. |
reset(screen, params?) |
Clears the stack and starts over at screen. |
goBack() |
Moves back one screen, or calls onRootBack at the first screen. |
The screen argument only selects the type of params. It does not change the current screen. Read params inside the component rendered for that screen: params always comes from the current screen, so a screen still mounted during a transition can briefly see another screen's params.
| Prop | What it does |
|---|---|
currentScreen |
Screen to render. |
screens |
Map of screen name to component. |
fallback |
Rendered when currentScreen is not in screens. Defaults to null. |
Use reset when the next screen should not lead back into the previous flow:
const { reset } = useNav();
const signOut = () => {
reset("home");
};After sign-out, browser back does not reopen the authenticated screens.
resetNavigateis nowreset. It clears the stack and also replaces the current browser entry.goBackfollows browser history. At the first screen it leaves the page unlessonRootBackis set.RouterViewmounts screens as components. The old() => <Screen />form creates a new component on every render and remounts the screen.Routesacceptsinterfacedeclarations.
The CI runs these on every push and pull request:
bun install --frozen-lockfile
bun run lint
bun test --dom
bun run buildMIT. See LICENSE.