SPA concepts
The programs here that touch the page run in a browser and the lines under each one are what its console prints; the first one is plain TypeScript and runs anywhere. A command-line Node program has no document, so a page script started there stops with ReferenceError: document is not defined. The one program that fetches a user needs the lesson's server, so its line below is the answer that server gives, not a line a command-line run can print.
Routes as state
A single page app keeps one page shell and swaps content inside it. The current route is plain state, often just a string such as home or about, that decides which screen to show.
Treat routes as data first and address bar second. When state drives the screen, navigation becomes a simple assignment followed by a render. The program assigns the other route and prints it.
type Route = "home" | "about"; let route: Route = "home"; route = "about"; console.log(route);
about
Render functions
Each screen is a function that returns an HTML string for the current state. The shell assigns that string to the page and the visible content updates at once.
Pure render functions are easy to preview and test because the same input always yields the same markup. Events and loading live outside, rendering stays dumb and honest. The program renders the home screen and prints the markup the body now holds.
function renderHome(): string { return "<h1>Home</h1>"; } function render(next: string): void { document.body.innerHTML = next; } render(renderHome()); console.log(document.body.innerHTML);
<h1>Home</h1>
Load JSON
Most screens need server data, usually fetched as JSON after navigation. Load with a typed helper, wait for the answer, then pass the result into the render function.
Show a loading line first so the app never looks frozen. Empty, loading, ready, and failed are four honest states worth rendering on purpose. The program fetches one user through a relative path and prints the name from the answer.
type User = { name: string }; async function loadUser(id: string): Promise<User> { const res = await fetch("/users/" + id); return res.json() as Promise<User>; } async function main(): Promise<void> { const user = await loadUser("7"); console.log(user.name); } main();
Ada
Back button note
Users expect the browser back button to undo navigation, not to break the app. Sync route state with history so back and forward move between screens you already render.
Start simple: one listener that reads the new location and renders again. Deep linking then works for free because every route is just state your render already knows. The program dispatches a popstate event and prints the text the listener rendered.
function renderHome(): string { return "<h1>Home</h1>"; } function render(next: string): void { document.body.innerHTML = next; } window.addEventListener("popstate", () => { render(renderHome()); }); window.dispatchEvent(new Event("popstate")); console.log(document.body.textContent);
Home
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is the current route of a single page app?
Answer
Plain state, often a string, that decides which screen renders
The route as data keeps navigation to an assignment and a render, and the address bar follows the state.
-
Why keep render functions pure?
Answer
The same input always gives the same markup, so they are easy to test
Events and loading stay outside, so a render can be previewed and checked from a value alone.
-
Why is an empty state worth rendering on purpose?
Answer
A list with no rows would otherwise look like a broken page
Empty, loading, ready and failed are four honest states, and each one tells the reader what is happening.
-
What does the browser back button need from the app?
Answer
Route state synced with history, and a listener that renders again
A popstate listener reads the new location and renders, which is what makes back and forward move between screens.
-
What does this program print?
type Route = "home" | "about"; let route: Route = "home"; route = "about"; console.log(route);Answer
about
The assignment replaces the value, and the print shows the new route.
-
What does this program print?
function renderHome(): string { return "<h1>Home</h1>"; } function render(next: string): string { return "body: " + next; } console.log(render(renderHome()));Answer
body: <h1>Home</h1>
The pure render returns the markup as text, and the second function prefixes it.
-
What does this program print?
async function loadName(id: string): Promise<string> { return "user " + id; } async function main(): Promise<void> { const name = await loadName("7"); console.log(name.toUpperCase()); } main();Answer
USER 7
The loaded name is turned to upper case before it is printed.
-
What does this program print?
const route: string = "about"; const screen = route === "home" ? "Home" : "About"; console.log(screen);Answer
About
The route is not home, so the other branch of the choice gives the About screen.
-
What does the compiler say about this program?
type Route = "home" | "about"; let route: Route = "contact"; console.log(route);Answer
error TS2322: Type '"contact"' is not assignable to type 'Route'.
The union names two strings, and contact is not one of them.
-
What does the compiler say about this program?
type Route = "home" | "about"; let route: Route = "home"; route = "about"; route = "contact";Answer
error TS2322: Type '"contact"' is not assignable to type 'Route'.
Every assignment is checked against the union, and the third value is outside it.
Article author: Arthur Isaev