Session Plan
Introduction to React
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
JSX Fundamentals
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}
</>
);
}Components & CSS imports
Thinking in components
What are components?
Breaking down UI into reusable components
Hierarchy and composition of components
Writing your first static component
Creating a Functional Component
Returning JSX
Single Root Element Requirement
Fragments
Using components inside components
Use {} to execute JS inside JSX
{} to execute JS inside JSXEmbedding Expressions in JSX
Conditional Rendering
Setting attributes
Standard HTML attributes
className
Importing / exporting .jsx
default exports and named exports
Importing .css
Global styles vs. component-specific styles
CSS modules using Vite
Exercises
1. Setting up a new React project
Point your terminal at the folder where you manage your projects, e.g.
cd ~/SitesScaffold 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 createConfirm with
y+Enterthatnpxmay run the create tool
Open your IDE (probably VSCode) at the
hyf-frontend-reactfolder (or alternative name you chose)Run
npm installin a terminal as requested aboveEvery time you start development, run
npm run dev
Hints
Ctrl/Cmd+ click on the URL displayed in your terminal, it watches your files and updates automatically in your browserThe file you likely want to edit to follow the next exercises is
src/App.jsxWhen you add components, create a
src/componentsfolder and add a file per component
2. Writing static components
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"
3. Nesting components
Create a functional component called
Cardsthat renders two instances of theCardcomponents from the previous exercise, potentially inside asectionelement
4. Executing JavaScript in JSX
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
5. Setting properties such as className and placeholder
className and placeholderCreate a functional component called
Buttonthat renders abuttonelement with aclassNameprop set tobtn primaryCreate a functional component called
DangerButtonthat renders abuttonelement with a dangerous text and aclassNameprop set tobtn dangerCreate a functional component called
TextInputthat renders a text input field with a placeholder valueType something hereCreate a functional component called
ProfileImagethat renders an image of a personAdd all the components above to your app
6. Adding basic styling
Create a global CSS file that changes the color of all buttons to
blueAdd component styles for the
DangerButtonwhich makes it large and red
(Bonus) Create a simple component structure
Design a basic layout with
header,maincontent, andfooterCreate separate components for each section
Last updated