Skip to content

Instantly share code, notes, and snippets.

View TimraWork's full-sized avatar
🤚
Hi) Му name is Elmira

Timra TimraWork

🤚
Hi) Му name is Elmira
View GitHub Profile
@TimraWork
TimraWork / function.js
Last active May 21, 2020 08:19
React - jsx в компонент(свойства-элементы)
// Создадим компонент ROW c jsx-разметкой
const Row = ({ left, right }) => {
return (
<Grid container spacing={3}>
<Grid item xl={8} md={9}>
{left}
<Pager />
</Grid>
<Grid item xl={4} md={3}>
{right}
@TimraWork
TimraWork / pattern.js
Last active May 21, 2020 05:45
React оптимизация - рендер функция - js паттерн
// app.js
<Posts renderItem={ ({ title, date }) => `${title} <span class="date">(${date})</span>` }/>
//posts.js
return items.map((item) => {
const { id } = item;
const label = this.props.renderItem(item);
return (
@TimraWork
TimraWork / pattern.js
Last active May 21, 2020 06:20
React оптимизация - переиспользование компонента(функции) - js паттерн
// posts.js
componentDidMount() {
this.SwapiService.getPosts() // Функция Меняется - остальное не меняется при переиспользовании
}
// +++++++++++++++ Преобразование функции ++++++++++++++
// В app.js
import SwapiService from '../swapi-service';
export default class App extends Component {
swapiService = new SwapiService();
@TimraWork
TimraWork / func.js
Last active May 20, 2020 12:09
JS - переделать обычную функцию async await в стрелочную (arrow func) - чтобы не терялся this
async getPosts() {
//...
}
getPosts = async () => {
//...
}
@TimraWork
TimraWork / componentDidUpdate.js
Last active May 19, 2020 11:51
React - componentDidUpdate(prevProps, prevState)
componentDidUpdate(prevProps) {
if (this.props.postId !== prevProps.postId) {
this.updatePost();
}
}
@TimraWork
TimraWork / life_cycle_hooks.js
Last active May 19, 2020 09:49
React - методы жизненного цикла ( life cycle hooks ) - DidMount, WillUnmount, render
componentDidMount() {
this.updatePost();
}
componentDidUpdate(prevProps, prevState) {
console.log('componentWillUnmount()');
}
componentWillUnmount() {
console.log('componentWillUnmount()');
@TimraWork
TimraWork / show_hide.js
Created May 7, 2020 06:00
React - скрыть/показать блок с componentWillUnmount
export default class App extends Component {
state = {
showPost: true,
};
onTogglePost = () => {
this.setState(({ showPost }) => {
return {
showPost: !showPost,
@TimraWork
TimraWork / math_random.js
Last active June 6, 2020 01:23
JS - ф-я генерации случайного числа ( Math.random() и Math.floor() )
Math.random() // от 0 - 1 (1 не включительно) - число float (с плавающей точкой)
Math.random()*25 // от 0 - 24 (дробные)
// Math.floor() - округление вниз
Math.floor(Math.random()*25) // от 0 - 25 (целые)
Math.floor(Math.random()*25) + 3 // от 0 - 28 (начиная с 2)
@TimraWork
TimraWork / catch_error.js
Created May 6, 2020 04:13
React - отлов ошибки сервера ( catch error )
// Шаг1 — создать компонент-функцию для вывода ошибок
// Файл components/error.js
const Error = () => {
return (
<div className="error">
Произошла ошибка. <br /> Попробуйте обновить страницу.
</div>
);
};
@TimraWork
TimraWork / react_fragment.js
Created May 5, 2020 11:48
React - использования React.Fragment тега в качестве обертки для элемента на примере loading(прелоадера)
// Прелоадер для компонента
export default class Post extends Component {
SwapiService = new SwapiService();
state = {
post: {},
loading: true,
};
onPostLoaded = (post) => {