Intro to React
Last updated
Last updated
In this lesson, you will learn the basics of React.
Table of Contents:
React — a library for building reusable, composable, and scalable user-interfaces made up of components.
Component — a piece of the UI (user interface) that has its own logic and appearance. Components are functions that return JSX.
JSX — an extension of JavaScript that lets you write HTML in React components.
Component Composition — the process of combining smaller, reusable components together to create larger, more complex components
Root Component — the top-level component that all other components are children of. Typically called App
.
react-dom/client
— a React package that lets you render React components on the client (in the browser)
Prop — a piece of data passed from a parent component to a child component.
React is a JavaScript library for building user-interfaces (UI).
React focuses on building reusable components that can be composed into larger components. This allows developers to build applications that scale efficiently.
Need more reasons to want to learn React?
Compare and contrast these code snippets for creating a dynamic text element.
Vanilla JS with the DOM API (imperative style):
React (declarative style):
In React, we are able to return markup language (like HTML) from functions. This is called JSX (JavaScript XML).
Components let you split the UI into independent, reusable pieces, and think about each piece in isolation.
Component Composition is the process of combining smaller, reusable components together to create larger, more complex components
Vanilla JS with the DOM API (imperative style):
React (declarative style):
Notice that <Caption />
and <Picture />
start with a capital letter. That’s how you know it’s a React component.
React component names must always start with a capital letter, while HTML tags must be lowercase.
Now that you know at least some of the basic terms of React, let's look at actually building a React app.
All of our projects up until this point have used Vanilla JS. Today is the day where you get to create a vite project using React!!!
By default you will be given a counter app. Take a look around!
Every React project will have this rough structure:
index.html
— the HTML file served to the client. It loads src/main.jsx
.
src/main.jsx
— the entry point of the app. It uses the react-dom/client
package to render the root component App
into the DOM.
src/App.jsx
— the root component.
Notice the
.jsx
extension? Without it, we wouldn't be able to write JSX in this file.
How does all of this actually get to the screen? Head over to the main.jsx
file.
The primary purpose of this file is render the root component App
. To do so we:
Import a package called ReactDOM
Use the ReactDOM.createRoot
method to create a root
object.
Then we call root.render
and pass in the App
component.
This is mostly handled when Vite creates the project for you so no need to memorize it:
A few notes:
We're using the client
version of ReactDOM
(there is also a native
version for mobile).
React.StrictMode
is a wrapper-component that detects potential React-related in our application. It doesn't render anything visible.
React components are functions that return a single JSX element.
The first component that we create is typically called App
Component are functions that return JSX.
Note the capitalized name. All components must use PascalCasing.
Components must return a single surrounding element. Here, we return a header
.
When returning multiple elements, wrap the elements parentheses ()
.
React is at its best when we separate the UI into individual components and then combine them to create the entire UI.
To render a component inside another component, we use the name of the component as if it were a self-closing HTML tag: <ComponentName />
Q: Compare and Contrast how each of these components return their children. What do you notice?
Imagine we had this style rule defined in a CSS file:
We can add style by using the className
attribute. Note that we aren't using class
. In React, many of the HTML attributes that we've grown accustomed to will have slightly different names.
JSX let's you put markup into our JavaScript.
We can also insert JavaScript into our JSX using curly braces {}
.
Notice how the alt
attribute uses string concatenation!
In the above example, style={{}}
is not a special syntax, but a regular {}
object inside the style={ }
JSX curly braces. You can use the style
attribute when your styles depend on JavaScript variables.
What makes React so powerful is the ability to share data between components using props.
Every React function-component is passed an argument called props
. It is an object containing properties provided to the component by the parent.
In this example, the parent component is App
and it provides a pictureData
prop to each instance of the InstagramPost
component.
Q: What will this render?
Use array to store data
Render {array.map}
Give each element a key
that should be unique (using the index of the array is okay but not ideal)
JSX in our code (<h1>...</h1>
) cannot simply be executed by our browser. It must first be compiled (converted) to vanilla JS.
Vite is doing the heavy lifting when it comes to the rendering.