React
Hi! My name is Evgenia. And today I want to introduce you to the
basics of React. So, let's get started. React is a JavaScript
library for building user interfaces 📝
Why React?
Declarative
How?
What?
We describe the final state of the desired UI.
Component-Based
We build encapsulated components that manage their own state.
The main features of React are the declarative approach (when you
describe the final state of the desired UI. We don't provide
step-by-step instructions. Instead, we describe the final UI we want
to get).
React is Component-Based That means we build encapsulated
components that manage their own state. And then we compose them to
make complex UIs.
Installation
npx create-react-app my-app
Let's begin with installation of Create React App, it is the best
way to start building a new single-page application in React. To
create a project, run: npx create-react-app my-app
Now we see some folders: node_modules, public, src and package.json
with ready-made settings, dependencies and some scripts.
Runs the app in the development mode
npm run start
The page will reload if you make edits.
You will also see any lint errors in the console.
Npm run start runs the app in the development mode. The page will
reload if you make edits. You will also see any lint errors in the
console.
Creates a production build of the app in the build/ folder
npm run build
When you're ready to deploy to production, you'll have to run npm
run build, and it will create an optimized build of your app in the
build folder. Under the hood, it uses Babel and webpack.
JSX
const element = Hello, I'm a JSX element!
A syntax extension to JavaScript.
JSX produces React “elements” - the smallest building blocks of
React apps.
Some words about JSX. Let's consider this variable declaration. It
looks like HTML, but it is a syntax extension to JavaScript. It
produces React “elements” - the smallest building blocks of React
apps.
Components and Props
function FirstComponent(props) {
return My first component {props.name}
}
Function component accepts a single "props" object argument with
data and returns a React element.
So we can create function components. Function component accepts a
single "props" object argument with data and returns a React
element. We call such components “function components” because they
are literally JavaScript functions.
App.js
function App() {
return (
Animal riddles
);
}
App.css
.App {
padding: 30px;
}
Ok, lets' try to create our first application - an Animal riddles
app. We'll remove all unnecessary from the file App.js Let's write a
header – Animal riddles. Add some styles to App.js. And here it is!
Our header!
Riddle.js
import React from 'react';
import './Riddle.css';
function Riddle() {
return (
{`A lot of spots. \nA long, long neck \nA funny scarf \nIt's a...`}
Show answer
Giraffe!
);
}
export default Riddle;
The word Class is a reserved word, so we write className.
Then we add a text of a riddle, an answer and a funny picture. And some styles. And.. nothing happened! Why?
There is no problem, we just have to add this component to our App.js!
React automatically imports it, thanks!
Riddle.css
.Riddle {
display: flex;
justify-content: space-between;
width: 320px;
margin-bottom: 30px;
}
.Riddle-Content {
display: flex;
flex-direction: column;
justify-content: space-between;
white-space: pre-wrap;
width: 220px;
height: 150px;
}
.Riddle-Answer {
display: flex;
flex-direction: column;
align-items: center;
width: 100px;
}
.Riddle-AnswerText {
font-size: larger;
font-weight: 600;
margin: 0;
margin-bottom: 15px;
}
.Riddle-Img {
height: 100px;
}
.Riddle-Button {
font-weight: 600;
line-height: 0;
text-align: center;
color: #6200ee;
width: 140px;
padding: 20px 20px;
background-color: #fff;
border-radius: 5px;
border: 1px solid #6200ee;
outline: transparent;
box-shadow: 1px 3px 10px rgba(0, 0, 0, 0.15);
cursor: pointer;
transition: all 0.2s ease;
}
.Riddle-Button:active {
background-color: #e1cefc;
transform: translate(0, 1px);
box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.3);
}
.Riddle-Button:hover {
background-color: #e9dff8;
}
Hooks
import { useState } from 'react';
const [isVisible, setIsVisible] = useState(false);
useState returns a pair: the current state value and a function
that lets you update it.
The only argument of useState is the initial state.
And now we would like the button to show and hide the answer.
So, we have to use a special function, it's called hook. Hooks let you use state and other React features inside function components.
We'll use hook useState().
Here are created a state and a method that will change this state.
The only argument of useState is the initial state. And if it's true, we can see the answer, and if the state is false, the answer is hidden.
Let's add onClick, it's a function. We can write it here, but it's better to take it out separately. Let's call it handleClick, and it will change a visibility of the answer.
JavaScript expressions
You can put any valid JavaScript expression inside the curly braces
in JSX.
Using props
const riddles = [
{
text: `A lot of spots. \nA long, long neck \nA funny scarf \nIt's a...`,
answer: 'Giraffe!',
answerImgUrl: './giraffe.svg',
},
{
text: `Green and long \nWith many teeth. \nBeautiful smile — \nIt's a...`,
answer: 'Crocodile!',
answerImgUrl: './crocodile.svg',
},
{
text: `Lives in seas and rivers. \nHis hands are like two pincers. \nAs round as a cab. \nWho is it? — It's a...`,
answer: 'Crab!',
answerImgUrl: './crab.svg',
},
];
function App() {
return (
Animal riddles
);
}
Let's pass the value to the component using props. We create an array of riddles with answers and images.
Rendering Multiple Components
We loop through the array using the JavaScript map() function.
Keys help React identify which items have changed, are added, or
are removed.
Keys should be given to the elements inside the array to give the
elements a stable identity.