Skip to content

Instantly share code, notes, and snippets.

View r3dm1ke's full-sized avatar
🎯
Before software can be reusable it first has to be usable.

Michael Krasnov r3dm1ke

🎯
Before software can be reusable it first has to be usable.
View GitHub Profile
@r3dm1ke
r3dm1ke / App.js
Created February 12, 2020 18:39
Example with useCallback
import React, {useState, useMemo, useCallback} from 'react';
function App() {
const [length, set_length] = useState(3);
const [name, set_name] = useState('John Doe');
const on_name_changed = useCallback((e) => set_name(e.target.value), []);
const on_length_changed = useCallback((e) => set_length(Number(e.target.value)), []);
return (
@r3dm1ke
r3dm1ke / App.js
Created February 12, 2020 17:38
Example with useMemo
import React, {useState, useMemo} from 'react';
function App() {
const [length, set_length] = useState(3);
const [name, set_name] = useState('John Doe');
return (
<>
<input value={name} onChange={e => set_name(e.target.value)} />
<NameDisplay name={name}/>
@r3dm1ke
r3dm1ke / App.js
Created February 12, 2020 17:35
Example without useMemo
import React, {useState} from 'react';
function App() {
const [length, set_length] = useState(3);
const [name, set_name] = useState('John Doe');
return (
<>
<input value={name} onChange={e => set_name(e.target.value)} />
<NameDisplay name={name}/>
// Declaring variables
// ReasonML has immutable variables by default
let name = 'Michael';
// Has the same syntax for object destructuring
let {user, password} = request;
// Control flow
let sum = (a, b) => a + b;
// ReasonML knows that the + operator is only applicable to integers
// (every type has its own operators), so this results in:
let sum = (a: int, b: int): int => a + b;
@r3dm1ke
r3dm1ke / type-inference.ts
Created February 8, 2020 16:49
Type inference in TypeScript
function add(x, y) {
return x + y;
}
// Type inference in TypeScript will result in this:
function add(x: any, y: any):any {
return x + y;
}
@r3dm1ke
r3dm1ke / question.js
Created February 7, 2020 17:40
What would this code return?
function abacaba() {
return
{
a: 'b'
}
}
console.log(abacaba());
@r3dm1ke
r3dm1ke / strict-mode.js
Created February 7, 2020 17:36
Example of code working in regular mode but failing in strict mode
x = 'abacaba'; // using an undeclared variable is not allowed in strict mode
delete x; // deleting variables is also not allowed
function(x1, x1) {} // duplicating argument names is not allowed
@r3dm1ke
r3dm1ke / promises.js
Last active August 14, 2020 08:01
Converting a callback into a Promise
// the function itself
function getData(callback, errorCallback) {
try {
// Do some network/api stuff...
callback(result)
} catch (e) {
errorCallback(e);
}
}
@r3dm1ke
r3dm1ke / constructor.js
Created February 6, 2020 18:39
Explaining JS constructor functions
function Car(name, make) {
// Here, this is not a reference to outer object
// But a placeheloder object you can use to construct the
// desired value
this.name = name;
this.make = make;
// you do not have to return anything, as this is automatically returned
}
const myCar = new Car('Outback', 'Subaru');