Skip to content

Instantly share code, notes, and snippets.

@Goloburda
Goloburda / index.js
Last active July 11, 2021 15:45
Micro/Macro tasks
// https://www.youtube.com/watch?v=cCOL7MC4Pl0
const button = document.getElementsByTagName('button')[0]
button.addEventListener('click', () => {
setTimeout(() => console.log('Timeout 1'))
Promise.resolve().then(() => console.log('Microtask - 1'))
console.log('task 1')
})
@Goloburda
Goloburda / index.js
Last active May 24, 2020 19:18
for await ()
function makeRequest(arg) {
return new Promise(res => {
setTimeout(() => res(arg), 2000);
});
}
const person = {
name() {
return makeRequest("Mikita");
},
age() {
@Goloburda
Goloburda / index.js
Last active April 30, 2020 16:06
[JavaScript] Filter array of objects by multiple params.
const arr = [
{name: "Mikita", age: 1, city: "Hrodna"},
{name: "Martin", age: 2, city: "Berlin"},
{name: "Nikita", age: 3, city: "Hrodna"},
{name: "Nikita", age: 4, city: "Hrodna"},
{name: "Sasha", age: 2, city: "Minsk"},
{name: "Sasha", age: 2, city: "Paris"},
{name: "Sasha", age: 2, city: "Hrodna"},
{name: "Martin", age: 2, city: "Berlin"},
{name: "Martin", age: 2, city: "London"},
@Goloburda
Goloburda / index.js
Created March 2, 2020 09:18
React Suspense?
const cache = new Map();
const setCallback = (element, key) => {
if (cache.has(key)) {
return cache.get(key);
}
throw element;
};
@Goloburda
Goloburda / useCallback.js
Created February 21, 2020 14:45
React useCallback
import React, { useState } from "react";
import Child from "./Child";
import "./styles.css";
const MyReact = {
storage: {},
myCallback: function(fn) {
if (this.storage.fn === undefined) {
this.storage.fn = fn;
}
@Goloburda
Goloburda / thisIsMutable.js
Created February 3, 2020 14:25
this is a mutable object.
const obj = {
name: "Mikita",
setName: function(name) {
this.name = name;
},
asyncName: function() {
const { name } = this;
setTimeout(() => {
console.log("async", name);
console.log("async2", this.name);
@Goloburda
Goloburda / style.js
Last active January 31, 2020 14:30
withStyles Material UI.
//Component to style doc: https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/Switch/Switch.js
import Switch from "@material-ui/core/Switch";
import styled from "styled-components";
import { withStyles } from "@material-ui/core/styles";
const StyledSwitch = styled(Switch)`
.MuiSwitch-colorSecondary {
color: green;
:hover {
@Goloburda
Goloburda / hoisting.js
Last active January 4, 2020 13:19
React functional component function hoisting.
const App = () => {
// Ugly. I always want to declear hooks at the top.
// But I can't swap getDefaultValue and useState (error getDefaultValue is't a function),
// because I use function assigmnet and in that way function does't hoisting.
const getDefaultValue = () => {
return "Default value";
};
const [title, setTitle] = useState(getDefaultValue());
@Goloburda
Goloburda / setState.js
Created December 18, 2019 10:47
React setState
/*
The setState() method does not immediately update the state of the component,
it just puts the update in a queue to be processed later.
React may batch multiple update requests together to make rendering more efficient.
Due to this, special precautions must be made when you try to update
the state based on the component's previous state.
*/
state = { count: 1 };
componentDidMount() {
@Goloburda
Goloburda / pipe.js
Created November 28, 2019 14:27
Allow to apply number of functions to passed argument.
const fn1 = arg => arg.toUpperCase();
const fn2 = arg =>
arg
.split("")
.reverse()
.join("");
const fn3 = arg => arg + "WoW";