Declarative UI and Component Thinking
The shared mental model behind React, Vue, Angular and Svelte: UI as a function of state, split into reusable components.
Beginner · about 3 hours · difficulty 2/5
Learn first: DOM Manipulation, Events and Event Delegation, JavaScript Modules
Building anything non-trivial with raw DOM calls gets painful: you must remember to update every element whenever data changes, and bugs appear when the screen and the data disagree. UI frameworks fix this with one idea: describe what the UI should look like for a given state, and let the framework update the DOM.
The shared concepts
- Components: self-contained pieces of UI (a
SearchBar, aProductCard) with their own markup, styles and behaviour, composed into trees. - Props / inputs: data passed down from parent to child. Children should not mutate them.
- State: data a component owns that changes over time. When it changes, the component re-renders.
- Events up, data down: children notify parents through callbacks or emitted events; parents own the shared state.
- Reactivity: the mechanism that notices state changes (virtual DOM diffing in React, signals and proxies in Vue, Svelte and Angular).
- Lifecycle and effects: code that runs when a component appears, updates or disappears.
Thinking in components
Start from a mockup. Draw boxes around each piece, name them, decide which component owns each piece of state (the closest common parent of everything that needs it), and pass the rest down as props. Keep state minimal: derive values like totals or filtered lists instead of storing them.
Pitfalls
Duplicated state that can drift apart, components that do too much, and deep prop chains that suggest a missing abstraction.
Good enough to move on
Given a mockup, you can sketch a component tree, say where each piece of state lives and which props and callbacks flow between components, independent of any framework.
Key concepts
- Declarative vs imperative UI
- Components and composition
- Props and one-way data flow
- State and re-rendering
- Lifting state up
- Derived state
- Virtual DOM vs signals
Mini project
Take a real product page (an e-commerce listing or a chat app), draw its component tree on paper, mark where each piece of state lives, then build a small part of it in vanilla JavaScript with a render(state) function.
Check yourself
Two sibling components both need the same selected item. Where should that state live, and why?
Free resources
- Thinking in React (react.dev)
- Introduction to client-side frameworks (MDN)