React Native

Содержание

Слайд 2

ПЛАН

1. Что такое React Native?
2. История React Native
3. React по сравнению с

ПЛАН 1. Что такое React Native? 2. История React Native 3. React
React Native
4. Что такое кросс-платформенная разработка?
5. Как работает React Native?
6. React Dev Tools
7. Virtual DOM
8. Альтернативы

Слайд 3

Компоненты JSX CRA

Компоненты JSX CRA

Слайд 4

Create React App

$ npx create-react-app myapp --use-npm
$ cd myapp/
$ npm start

Create React App $ npx create-react-app myapp --use-npm $ cd myapp/ $ npm start

Слайд 5

Компонент

// Pokemon.js
function Pokemon() {
return 'some pokemon';
}
export default Pokemon;

// App.js
import React from

Компонент // Pokemon.js function Pokemon() { return 'some pokemon'; } export default
'react';
import Pokemon from './Pokemon';
function App() {
return (



);
}
export default App;

Слайд 6

JSX

// Pokemon.js
import React from 'react';
function Pokemon() {
return (
<>

Slowpoke


Вес:

JSX // Pokemon.js import React from 'react'; function Pokemon() { return (
360 гектограмм
src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/79.png"
alt="Slowpoke"
/>

);
}
export default Pokemon;

Слайд 8

Props

// Pokemon.js
import React from 'react';
function Pokemon({ name, weight, img }) {
return

Props // Pokemon.js import React from 'react'; function Pokemon({ name, weight, img
(
<>

{name}



Вес:
{' '}
{weight}
{' '}
гектограмм

{name}

);
}
export default Pokemon;

// App.js
import React from 'react';
import Pokemon from './Pokemon';
function App() {
return (


name="Slowpoke"
weight={360}
img="https://bit.ly/2LkFfgI"
/>

);
}
export default App;

Слайд 9

map && ?:

map && ?:

Слайд 10

map, key

// App.js
import React from 'react';
import Pokemon from './Pokemon';
function App() {
const

map, key // App.js import React from 'react'; import Pokemon from './Pokemon';
pokemons = [
{ name: 'Slowpoke', weight: 360, img: 'https://bit.ly/2LkFfgI' },
{ name: 'Pikachu', weight: 60, img: 'https://bit.ly/3co9xem' },
{ name: 'Psyduck', weight: 196, img: 'https://bit.ly/2Wldz1g' },
];
return (

{pokemons.map(({ name, weight, img }) => (
key={name}
name={name}
weight={weight}
img={img}
/>
))}

);
}
export default App;

Слайд 11

&&

// Pokemon.js
import React from 'react';
function Pokemon({ name, weight = false, img })

&& // Pokemon.js import React from 'react'; function Pokemon({ name, weight =
{
return (
<>

{name}


{
weight
&& (

Вес:
{' '}
{weight}
{' '}
гектограмм

)
}
{name}

);
}
export default Pokemon;

Слайд 12

?:

// Pokemon.js
import React from 'react';
function Pokemon({ name, weight = false, img })

?: // Pokemon.js import React from 'react'; function Pokemon({ name, weight =
{
return (
<>

{name}


{
weight
? (

Вес:
{' '}
{weight}
{' '}
гектограмм

)
: Вес не определён
}
{name}

);
}
export default Pokemon;

Слайд 14

useState (+functional updates)

// Pokemon.js
import React, { useState } from 'react';
const weightStep =

useState (+functional updates) // Pokemon.js import React, { useState } from 'react';
50;
function Pokemon({ name, weight, img }) {
const [
currentWeight,
setCurrentWeight,
] = useState(weight);
function addWeight() {
setCurrentWeight((x) => x + weightStep);
}
function removeWeight() {
setCurrentWeight((x) => x - weightStep);
}
return (
<>

{name}



Вес:
{' '}
{currentWeight}
{' '}
гектограмм



width={96 * (currentWeight / weight)}
src={img}
alt={name}
style={{ display: 'block' }}
/>

);
}
export default Pokemon;

Слайд 15

useEffect

useEffect

Слайд 16

useEffect

// App.js
import React, { useState, useEffect } from 'react';
function App() {
const

useEffect // App.js import React, { useState, useEffect } from 'react'; function
[date, setDate] = useState(new Date());
useEffect(() => {
const timer = setTimeout(() => setDate(new Date()), 1000);
return () => clearTimeout(timer);
}, [date]);
return (

{date.toLocaleString()}

);
}
export default App;

Слайд 17

useEffect (practical example)

// App.js
import React from 'react';
import PokemonList from './PokemonList';
function App() {

useEffect (practical example) // App.js import React from 'react'; import PokemonList from
return (



);
}
export default App;
// lib/fetch-all.js
export default async (urls, options) => {
const responses = await Promise.all(
urls.map((url) => fetch(
url,
options,
)),
);
return Promise.all(
responses.map((response) => response.json()),
);
};

// PokemonList.js
import React, { useState, useEffect } from 'react';
import fetchAll from './lib/fetch-all';
import Pokemon from './Pokemon';
function PokemonList({ pokemons }) {
const [pokemonsData, setPokemonsData] = useState([]);
useEffect(() => {
(async () => {
const jsonResults = await fetchAll(
pokemons.map((name) => `https://pokeapi.co/api/v2/pokemon/${name}/`),
);
return setPokemonsData(jsonResults.map((data) => ({
name: data.name,
weight: data.weight,
img: data.sprites.front_default,
})));
})();
}, [pokemons]);
return pokemonsData.map(({ name, weight, img }) => (

));
}
export default PokemonList;

Слайд 18

useEffect (clean-up)

// App.js
import React from 'react';
import PokemonList from './PokemonList';
function App() {
return

useEffect (clean-up) // App.js import React from 'react'; import PokemonList from './PokemonList';
(



);
}
export default App;
// lib/fetch-all.js
export default async (urls, options) => {
const responses = await Promise.all(
urls.map((url) => fetch(
url,
options,
)),
);
return Promise.all(
responses.map((response) => response.json()),
);
};

// PokemonList.js
import React, { useState, useEffect } from 'react';
import fetchAll from './lib/fetch-all';
import Pokemon from './Pokemon';
function PokemonList({ pokemons }) {
const [pokemonsData, setPokemonsData] = useState([]);
useEffect(() => {
const abortController = new AbortController();
const { signal } = abortController;
(async () => {
let jsonResults;
try {
jsonResults = await fetchAll(
pokemons.map((name) => `https://pokeapi.co/api/v2/pokemon/${name}/`),
{ signal },
);
} catch (err) {
return setPokemonsData([]);
}
return setPokemonsData(jsonResults.map((data) => ({
name: data.name,
weight: data.weight,
img: data.sprites.front_default,
})));
})();
return () => abortController.abort();
}, [pokemons]);
return pokemonsData.map(({ name, weight, img }) => (

));
}
export default PokemonList;

Слайд 19

React Dev Tools

React Dev Tools

Слайд 20

React Dev Tools
https://github.com/facebook/react/tree/master/packages/react-devtools

React Dev Tools https://github.com/facebook/react/tree/master/packages/react-devtools

Слайд 21

Virtual DOM

Virtual DOM

Слайд 22

Virtual DOM
Reconciliation
Diffing
Keys

Virtual DOM Reconciliation Diffing Keys

Слайд 23

Альтернативы

Альтернативы
Имя файла: React-Native.pptx
Количество просмотров: 30
Количество скачиваний: 0