React Introduction
React is a JavaScript library for building user interfaces. Instead of writing step-by-step instructions that poke at the page ("find this element, change its text, add a class"), you describe what the screen should look like for the current data, and React works out the minimum set of DOM changes needed to get there.
React was created at Facebook in 2013 and is now maintained by Meta together with a large open-source community. This course teaches React 19, the current major version.
The big ideas
| Idea | What it means |
|---|---|
| Components | The UI is split into small, reusable functions such as <Header />, <Cart /> or <Button /> |
| Declarative | You write what the UI should be for a given state; React handles the updates |
| State | Data that changes over time lives in state; when it changes, React re-renders |
| One-way data flow | Data travels down from parent to child through props, which keeps apps predictable |
| JSX | An HTML-like syntax inside JavaScript that describes the UI |
Your first React component
A React component is just a JavaScript function that returns some markup. Press ▶ Run to render it:
export default function App() {
return (
<div>
<h1>Hello, React!</h1>
<p>This paragraph was rendered by a component.</p>
</div>
);
}That <h1> inside JavaScript is JSX. It looks like HTML, but it is compiled into ordinary function calls. You will learn every detail of it in the JSX chapter.
Tip: On ExcelTAD a
jsxplayground that hasexport defaultis mounted automatically into a<div id="root">. Edit the code and press Ctrl+Enter to see your change.
Why not just use plain JavaScript?
Imagine a counter button written with the DOM API. You need to find the element, keep a variable, attach a listener and remember to update the text every time the variable changes. With React you only describe the result:
import { useState } from 'react';
export default function App() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)} style={{ fontSize: 18, padding: '8px 16px' }}>
Clicked {count} {count === 1 ? 'time' : 'times'}
</button>
);
}Clicking the button changes the count state. React calls App again, compares the new output with the old one and updates only the text of the button. You never touch the DOM yourself.
Components can be combined
Big interfaces are built by nesting small components, just like HTML tags:
function Avatar() {
return <div style={{ fontSize: 48 }}>🦊</div>;
}
function Profile() {
return (
<div style={{ border: '1px solid #ccc', borderRadius: 8, padding: 12, width: 180, textAlign: 'center' }}>
<Avatar />
<strong>Fox McCloud</strong>
<p>Frontend developer</p>
</div>
);
}
export default function App() {
return (
<div style={{ display: 'flex', gap: 12 }}>
<Profile />
<Profile />
</div>
);
}What is React used for?
- Single-page web applications (dashboards, editors, social networks)
- Parts of existing websites — React can control just one widget on a page
- Full-stack apps with frameworks such as Next.js or React Router
- Mobile apps with React Native, which uses the same component model
What you should know first
React is "just JavaScript", so you will learn faster if you are comfortable with:
- HTML and CSS basics
- JavaScript functions, arrays, objects and modules
- Modern syntax: arrow functions, destructuring, spread, template literals
Don't worry if some of these are rusty: the ES6 Refresher lesson in this course recaps everything React code uses.
Library vs. framework
React itself only handles rendering components and managing their state. It does not include routing, data fetching or a build system. You combine it with other tools (Vite, a router, a data library) or use a framework built on React that bundles these choices for you.
Test yourself
What is a React component?
- A CSS file
- A function that returns UI markup
- An HTML template stored on the server
- A browser extension
What does 'declarative' mean in React?
- You manually update the DOM after every change
- You describe what the UI should look like for the current state
- You declare all variables with const
- You write the UI in XML files
In which direction does data flow through props?
- From child to parent
- Between siblings only
- From parent to child
- In both directions automatically