Client
Server
Database
HTTP
browser
request
html page
Client
Server
Database
HTTP
React in browser, mobile app...
API
request
data
Single page application
Web server
html, js
npm create vite@latest
Project name: … (react-course)
Select a framework: › React
Select a variant: › Typescript
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
</React.StrictMode>,
)
npm run dev
const label = React.createElement('a', {
href: 'https://google.com'
}, 'Go to Google.com');
<a href="https://google.com">Go to Google.com</a>
children
props
type
import React from 'react';
function App() {
return (
<div className="App">
Hello
</div>
);
}
function App() {
return React.createElement('div', { className: 'App' }, 'Hello');
}
import React from 'react';
function App() {
return (
<div className="App">
Hello
</div>
);
}
function App() {
return React.createElement('div', { className: 'App'}, 'Hello');
}
import React from 'react';
function App() {
return (
<div>
Yes
</div>
<div>
No
</div>
);
}
function App() {
return ????????
}
import React from 'react';
function App() {
return (
<>
<div>
Yes
</div>
<div>
No
</div>
</>
);
}
function App() {
return <h1>Hello</h1>;
}
function App() {
let something = 'hello';
return <div>{something}</div>;
}
function Array() {
let array = [1,2,3];
return <div>
{array.map((item, index) => <span key={index}>{item}</span>)}
</div>;
}
‼️ key prop
type Props = {
name: string;
};
function NameComponent(props: Props) {
return <h1>Hi, my name is {props.name}!</h1>;
}
ReactDOM.render(
<NameComponent name="Martin" />,
document.getElementById('root')
);
Component
Component
Component
Component
Component
Component
Component
User info
ArticleList
Article
Today Weather
Article
I am smart 💡
function NameComponent(props) {
return <h1>{props.name}</h1>;
}
function App() {
return <NameComponent name="Martin" />
}
<button type="button" onClick={() => console.log('Hello')}>
Hello world
</button>
1 | 2 | 3 |
---|---|---|
4 | 5 | 6 |
7 |
const input = [1,2,3,4,5,6,7]
<Table columns={3} array={input} />
1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|
1️⃣
const input = [1,2,3,4,5,6,7]
<Table array={input} />
2️⃣
Tatranka
Fidorka
Mars
Price: 20,-
Coins: 50,-
🪙
Insert coin
3x
5x
0x
out of stock
Tatranka
Fidorka
Mars
Tatranka
Fidorka
Mars
function VendingMachine() {
const items = [
'Tatranka',
'Fidorka',
'Mars'
];
return ...
}
function VendingItem() {
return <button />{name}
}
1x
1x
1x
import React, { useState } from 'react';
function Counter() {
const [name, setName] = useState('nobody');
function handleGiveName(name: string) {
setName(name);
}
return <div>
My name is {name}.
<button onClick={() => handleGiveName('Martin')}>
Give me name
</button>
</div>
}
initial value
Tatranka
Fidorka
Mars
3x
5x
0x
import React from 'react';
type State = {
counter: number;
}
export class MyComponent extends React.Component<{}, State> {
state = {
counter: 0
};
increment() {
this.setState({ counter: this.state.counter + 1 });
}
render() {
const { counter } = this.state;
return <div>
Counter: {counter}
<button type="button" onClick={() => this.increment()}>Increment</button>
</div>
}
}
type Props = {
pregeneratedCount: number
}
type State = {
generatedNumbers: number[];
}
export class NumberGeneratorClass extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
const generatedNumbers = [...Array(props.pregeneratedCount)].map(() => Math.random());
this.state = {
generatedNumbers
};
}
generateNew() {
this.setState({ generatedNumbers: [...this.state.generatedNumbers, Math.random()] });
}
render() {
const { generatedNumbers } = this.state;
return <div>
{generatedNumbers.map((num, index) => <div key={index}>{num}</div>)}
<button type="button" onClick={() => this.generateNew()}>Generate new</button>
</div>
}
}
function MyComponent() {
const random = Math.random();
if (random < 0.5) {
return <span>lower</span>
} else {
return <span>higher</span>
}
}
function MyComponent() {
const random = Math.random();
return <span>
{random < 0.5 ? 'lower' : 'higher'}
</span>
}
function MyComponent() {
const condition = true;
return <>
{condition && <span>It's true</span>}
</>
}
Tatranka
Fidorka
Mars
3x
5x
0x
out of stock
<div style={{color: 'red'}}>Out of stock</div>
debugger;
import './App.css';
function Component() {
return <div className="red">Hello</div>
}
.red {
color: red;
}
App.css
App.tsx
import styles from './Component.module.css';
function Component() {
return <div className={styles.red}>Hello</div>
}
.red {
color: red;
}
App.module.css
App.tsx
import cn from 'classnames';
function ValidationState() {
const [invalid, setInvalid] = useState(false);
return <div className={cn({ red: invalid })}>
Status
</div>
}
import cn from 'classnames';
import styles from './ValidationState.module.css';
function ValidationState() {
const [invalid, setInvalid] = useState(false);
return <div className={cn({ [styles.red]: invalid })}>
Status
</div>
}
Tatranka
Fidorka
Mars
3x
5x
0x
out of stock
export const MyMouse = () => {
const [mousePosition, setMousePosition] = useState({x: 0, y: 0});
useEffect(() => {
const onMouseMove = (event: MouseEvent) => {
setMousePosition({
x: event.clientX,
y: event.clientY
});
};
window.addEventListener('mousemove', onMouseMove);
return () => {
window.removeEventListener('mousemove', onMouseMove);
};
}, []);
const {x, y} = mousePosition;
return (
<div>My mouse x position is {x} and y position is {y}</div>
);
};
Component
User info
ArticleList
Article
Today Weather
Article
I am smart, I know what to do 💡
❤️ "User liked an article"
type Props = {
article: Article;
onLike: () => void;
};
function Article(props: Props) {
return <>
<h1>{article.title}</h1>
<p>{article.shortText}</p>
<button onClick={props.onLike}>❤️ Like</button>;
</>
}
<Article article={article} onLike={() => handleLike(article)} />
parent component:
child component:
Tatranka
Fidorka
Mars
3x
5x
0x
out of stock
Price: 20,-
<MyBytton onClick={...}>
<Icon> Click me!
</MyBytton>
function MyButton(props) {
return (
<button className="blue-button" onClick={props.onClick}>
{props.children}
</button>
)
}
<DropdownComponent label="Open dropdown">
Hello, this is dropdown!
</DropdownComponent>
Tatranka
Fidorka
Mars
🪙
Insert coin
3x
5x
0x
out of stock
+10 coins
+20 coins
+50 coins
Coins: 50,-
Price: 20,-
<DropdownComponent label="Insert coins">
<button>+ 10 coins</button>
<button>+ 20 coins</button>
<button>+ 30 coins</button>
</DropdownComponent>
function Component() {
const [inputName, setInputName] = useState(name);
return <>
{inputName}
<input
value={inputName}
onChange={(e) => setInputName(e.target.value)} />
</>
}
Tatranka
Fidorka
Mars
🪙
Insert coin
3x
5x
0x
out of stock
+10 coins
+20 coins
+50 coins
Any amount
Coins: 50,-
Price: 20,-
+10 coins
+20 coins
+50 coins
Any amount
Tatranka
Fidorka
Mars
Price: 20,-
Coins: 50,-
🪙
Insert coin
3x
5x
0x
out of stock
import axios from 'axios';
const payload = { name: 'Martin' };
const response = await axios.post('/api/users', payload);
console.log(response);
import axios from 'axios';
const payload = { name: 'Martin' };
axios.post('/api/users', payload)
.then(response => console.log(response));
type Response = {
id: number;
name: string;
age: number;
}
axios.get<Response>('/api/users/1')
GET https://api.chucknorris.io/jokes/random
const useMouseMove = () => {
const [mousePosition, setMousePosition] = useState({ x: 0, y: 0 });
useEffect(() => {
const onMouseMove = (event: MouseEvent) => {
setMousePosition({
x: event.clientX,
y: event.clientY,
});
};
window.addEventListener('mousemove', onMouseMove);
return () => {
window.removeEventListener('mousemove', onMouseMove);
};
}, []);
return { x: mousePosition.x, y: mousePosition.y };
};
type ContextValue = boolean;
const MyContext = React.createContext<ContextValue>(false);
function App() {
return <MyContext.Provider value={true}>
<Component />
</MyContext.Provider>;
}
function Component() {
const value = useContext(MyContext);
return <div>{value}</div>;
}
type ContextValue = {
value: number;
setValue: (value: ContextValue['value']) => void
};
const MyContext = React.createContext<ContextValue>({} as unknown as ContextValue);
function App() {
const [value, setValue) = useState(0);
return <MyContext.Provider value={{ value, setValue}}>
<Component />
</MyContext.Provider>;
}
function Component() {
const {value, setValue} = useContext(MyContext);
return <>
<div>{value}</div>
<button onClick={() => setValue(value + 1)}>Click</button>
</>;
}
Tatranka
Fidorka
Mars
Price: 20,-
Coins: 50,-
🪙
Insert coin
3x
5x
0x
out of stock
function Component() {
const inputRef = useRef<HTMLInputElement>(null);
function handleClick() {
inputRef.current.focus();
}
return <div>
<input ref={inputRef} />
<button onClick={handleClick}>Focus the input</button>
</div>
}