Skip to content

Instantly share code, notes, and snippets.

@Goloburda
Goloburda / javascriptClosure.js
Created November 26, 2019 08:07
JavaScript call function n-times.
// Function calls n-times before get '()';
const closureFn = firstArgument => {
let sum = firstArgument;
return function callMe(nextArgument) {
if (nextArgument) {
sum += nextArgument;
return callMe;
}
return sum;
@Goloburda
Goloburda / keywordthis.js
Created November 26, 2019 08:35
Keyword "this". JavaScript.
"use strict";
console.log(this); //undefined
function Person(name, age) {
this.name = name;
this.age = age;
this.sayName = () => {
console.log(this.name);
};
@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";
@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 / 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 / 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 / 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 / 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 / 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 / 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"},