State and Components: How UI Frameworks Think
Understand the idea behind React, Vue and Svelte before you use them: UI as a function of state, one source of truth, render functions, components and props, events that change state, and when to reach for a framework. Practise by building a to-do list with plain JavaScript.
1. The problem frameworks solve
You can build interactivity by finding an element and changing it: add a list item, update a counter, show an error. For two or three features that works. As the page grows, the same fact (say, “how many items are in the cart”) ends up displayed in several places, and every change must remember to update all of them. Miss one and the screen disagrees with itself.
Frameworks such as React, Vue, Svelte and Angular are all answers to one idea:
UI is a function of state. Keep your data in one place (the state). Describe how the screen looks for any given state. When the state changes, the screen is updated to match.
You can use that idea without any framework, which is the best way to understand what they do for you.
2. State and render
const state = { todos: ['Learn CSS', 'Read about the DOM'] }; // the single source of truth
function render() {
const list = document.getElementById('list');
list.replaceChildren(); // clear
for (const text of state.todos) {
const li = document.createElement('li');
li.textContent = text; // text, never innerHTML, for user data
list.append(li);
}
document.getElementById('count').textContent = `${state.todos.length} items`;
}
Rules:
- State lives in one place (an object, a variable), not scattered across the DOM. Do not read values back from the page to know what is true.
render()is the only code that touches the DOM for that part of the screen, and it can be run again at any time.- Events change the state, then call
render()(or let a framework do so):
function addTodo(text) {
state.todos = [...state.todos, text]; // a new array rather than push: clearer, and what frameworks expect
render();
}
document.getElementById('add').addEventListener('click', () => {
const input = document.getElementById('new');
const text = input.value.trim();
if (!text) return; // validate input
addTodo(text);
input.value = '';
});
This is called unidirectional data flow: events go up to change state, state flows down into the view.
3. Components and props
A component is a reusable piece of UI with its own markup, style and behaviour. Without a framework it is just a function that returns an element:
function Card({ title, body }) { // props: the inputs
const el = document.createElement('article');
el.className = 'card';
const h = document.createElement('h3'); h.textContent = title;
const p = document.createElement('p'); p.textContent = body;
el.append(h, p);
return el;
}
app.append(Card({ title: 'Networking', body: 'Start with the OSI model.' }));
The caller passes props (data in); the component does not care where the data came from. In frameworks, the same idea looks like this:
// React
function Card({ title, body }) {
return <article className="card"><h3>{title}</h3><p>{body}</p></article>;
}
<!-- Svelte -->
<script>export let title; export let body;</script>
<article class="card"><h3>{title}</h3><p>{body}</p></article>
Build screens by composing small components: a CourseList contains many Cards. Data flows down as props; events flow up as callbacks or custom events.
4. What a framework adds
A framework takes over the tedious and error-prone parts:
| Job | By hand | With a framework |
|---|---|---|
| Update the screen after a change | Call render and rebuild everything | It works out the minimum DOM changes (virtual DOM or compile-time updates) |
| Escape user text | You must remember textContent |
Escaped by default |
| Re-use UI | Functions that build elements | Components with their own scope and styles |
| Share state | Globals you must keep tidy | Stores, context, signals |
| Routing, forms, tooling | Build it yourself | Ecosystem and conventions |
The main choices
- React: the biggest ecosystem and most jobs; JSX; hooks (
useState,useEffect); a virtual DOM. - Vue: approachable, single-file components, reactive data.
- Svelte: compiles components to small, fast code with very little boilerplate (this very site is built with it, together with Astro).
- Angular: a full framework with strong conventions, popular in large organisations.
- Astro / Next.js / Nuxt / SvelteKit: “meta-frameworks” that add routing, server rendering and builds on top.
Learn one well. The ideas (state, components, props, events, effects) transfer almost unchanged between them.
5. Where state should live
- Local state: only one component cares (is a menu open?). Keep it inside that component.
- Lifted state: two siblings need it. Move it to their closest common parent and pass it down as props.
- Global / shared state: many distant parts need it (the signed-in user, the cart). Use a store.
- Server state: data that really lives on a server (courses, orders). Fetch it, cache it and refresh it with a library built for that (TanStack Query, SWR), and do not duplicate it in your own state.
- URL state: things users should be able to bookmark or share (the search term, the page number) belong in the URL.
Derived data (such as the count of todos) should be computed from state, not stored separately, so it cannot get out of sync.
6. Security reminder
The most common front-end vulnerability is XSS: text from a user or an API inserted as HTML. Frameworks escape by default, and also give you escape hatches (innerHTML, dangerouslySetInnerHTML, {@html}, v-html). Treat every use of those as a security decision, and sanitise with a trusted library such as DOMPurify if you truly need to show HTML.
7. Your turn: a to-do list from state
Complete the script so that:
render()rebuilds#listfromstate.todos(one<li>per item, usingtextContent) and writesN itemsinto#count(for exactly one item it should say1 item).- Clicking
#addadds the text from#newto the state (as a new array), clears the input, and re-renders. A blank input is ignored.
Your tasks
- ○Initially #count says '0 items'
- ○Adding 'Learn CSS' creates one list item with that text and '1 item'
- ○The input is cleared after adding
- ○Adding a second item gives two items and '2 items'
- ○A blank entry is ignored
- ○The list is rebuilt, not appended to: no duplicates after another render
- ○Markup typed by a user is shown as text, not run (no <b> element is created)
8. What you learned
You keep state in one place, write a render() that turns state into screen, change state in response to events and re-render, and build reusable components that take props. You also know why frameworks exist, how to choose where state lives, and why user text must never be inserted as HTML. Next: the tools that make you faster at finding problems.
🧪 Want to practise this on your own computer? See how to set up your lab →