- Vanilla JavaScript
- interacts directly with the DOM
- this is fairly easy to maintain for small apps and sites – and if it's just you developing it
- React
- can enable you to add a bunch of other people to help you and collaborate to move faster
- can help you avoid repetition that's natural when interacting with the DOM a lot
- can update the UI in a reactive way (hence the name) when your data changes
- makes it possible to compose an application or website of small components that you can re-use
- React's strengths:
- Very suitable for working in teams and sharing those components (even publicly)
- Great choice for design systems, i.e. making components once and using them for all your (future) applications, across different devices
- There's a vast library of components already out there on npm and a big community of developers using it today
- React's weaknesses:
- May introduce too much overhead to small projects
- It's an abstraction – can be misused and slow down your page
- Need to learn JSX, hooks and other concepts
- You always have to return JSX from a component for it to work
- Returning JSX works like this
function MyComponent() {
return <h1>Hello World</h1>;
}- JSX looks a bit like HTML, and you need to use HTML elements to build components
- But JSX has some extra rules compared to plain HTML
- For example, you always have to return a single root element, that's a fixed requirement
// ⚠️ THIS WON'T WORK
function MyBrokenComponent() {
return (
<h1>Hello World</h1>
<h2>Hello Universe</h2>
)
}- You can use Fragments to address this issue
function MyComponent() {
return (
<React.Fragment>
<h1>Hello World</h1>
<h2>Hello Universe</h2>
</React.Fragment>
);
}– or –
function MyComponent() {
return (
<>
<h1>Hello World</h1>
<h2>Hello Universe</h2>
</>
);
}- JSX is really a templating language, enabling you to use JavaScript while generating HTML (kind of)
- You put JavaScript in
{}curly braces
function MyComponent() {
const firstName = "HackYour";
return <h1>Hello {firstName + "Future"}</h1>;
}- This mechanism also allows you to "render" conditionally, showing different content based on conditions you define
- Rendering is a fancy term for putting the HTML in the DOM that you describe in JSX, i.e. showing the component on the page
function MyComponent() {
const isSunday = new Date().getDay() === 7;
return (
<>
<h1>Hello!</h1>
{isSunday ? <h1>What a nice Sunday!</h1> : null}
</>
);
}- What are components?
- Breaking down UI into reusable components
- Hierarchy and composition of components
- Creating a Functional Component
- Returning JSX
- Single Root Element Requirement
- Fragments
- Using components inside components
- Embedding Expressions in JSX
- Conditional Rendering
- Standard HTML attributes
className
- default exports and named exports
- Global styles vs. component-specific styles
- CSS modules using Vite
- Point your terminal at the folder where you manage your projects, e.g.
cd ~/Sites - Scaffold a basic Vite React project by following the instructions
in the Vite Guide
- At the time of writing this, you can get started by running the following command
npm create vite@latest hyf-frontend-react -- --template react, wherehyf-frontend-reactis the project/folder name that the tool will create - Confirm with
y+Enterthatnpxmay run the create tool
- At the time of writing this, you can get started by running the following command
- Open your IDE (probably VSCode) at the
hyf-frontend-reactfolder (or alternative name you chose)- Run
npm installin a terminal as requested above - Every time you start development, run
npm run dev
- Run
- Hints
Ctrl/Cmd+ click on the URL displayed in your terminal, it watches your files and updates automatically in your browser- The file you likely want to edit to follow the next exercises is
src/App.jsx - When you add components, create a
src/componentsfolder and add a file per component
- Create a functional component called
Greetingthat returns anh1element with the text "Hello, React!" - Create a functional component called
Cardthat returns aarticleelement with a nestedh2element and apelement. Theh2should display the text "Card Title", and thepshould display the text "This is a card component"
- Create a functional component called
Cardsthat renders two instances of theCardcomponents from the previous exercise, potentially inside asectionelement
- Create a functional component called
Personcontaining two constantsnameandage. The component should display the text:{name} is an adultif the age is 18 or higher or{name} is a minorif the age is less than 18
- Create a functional component called
Buttonthat renders abuttonelement with aclassNameprop set tobtn primary - Create a functional component called
DangerButtonthat renders abuttonelement with a dangerous text and aclassNameprop set tobtn danger - Create a functional component called
TextInputthat renders a text input field with a placeholder valueType something here - Create a functional component called
ProfileImagethat renders an image of a person - Add all the components above to your app
- Create a global CSS file that changes the color of all buttons to
blue - Add component styles for the
DangerButtonwhich makes it large and red
- Design a basic layout with
header,maincontent, andfooter - Create separate components for each section