Skip to content

Instantly share code, notes, and snippets.

View davidicus's full-sized avatar

David Conner davidicus

View GitHub Profile
@davidicus
davidicus / uselayouteffect-ssr.md
Created October 31, 2023 11:38 — forked from gaearon/uselayouteffect-ssr.md
useLayoutEffect and server rendering

If you use server rendering, keep in mind that neither useLayoutEffect nor useEffect can run until the JavaScript is downloaded.

You might see a warning if you try to useLayoutEffect on the server. Here's two common ways to fix it.

Option 1: Convert to useEffect

If this effect isn't important for first render (i.e. if the UI still looks valid before it runs), then useEffect instead.

function MyComponent() {
@davidicus
davidicus / uselayouteffect-ssr.md
Created October 31, 2023 11:38 — forked from gaearon/uselayouteffect-ssr.md
useLayoutEffect and server rendering

If you use server rendering, keep in mind that neither useLayoutEffect nor useEffect can run until the JavaScript is downloaded.

You might see a warning if you try to useLayoutEffect on the server. Here's two common ways to fix it.

Option 1: Convert to useEffect

If this effect isn't important for first render (i.e. if the UI still looks valid before it runs), then useEffect instead.

function MyComponent() {
@davidicus
davidicus / dnDUploadConversion.js
Created November 12, 2020 01:07
Drag and drop file upload and conversion to data URL
const handleOnChange = (e, callback) => {
console.log('onChanged');
const fReader = new FileReader();
fReader.readAsDataURL(
e.type === 'drop' ? e.dataTransfer.files[0] : e.target.files[0]
);
fReader.onloadend = (event) => {
callback(event.target.result);
};
};
@davidicus
davidicus / fetchImgFromURL.js
Created November 11, 2020 04:47
Get an image from a URL and covert it into a data url
const fetchDataURL = (url) =>
fetch(url)
.then((res) => res.arrayBuffer())
.then((ab) =>
btoa(
new Uint8Array(ab).reduce(
(data, byte) => data + String.fromCharCode(byte),
''
)
)
@davidicus
davidicus / cssCustomPropertyFallback.scss
Last active April 3, 2020 19:34
Sass mixin for non support fallbacks with custom properties
:root {
--foo: green;
}
$ui-01: #555;
@mixin customProp($property, $css-value, $fallback-value) {
#{$property}: $fallback-value;
#{$property}: var(#{$css-value});
}
@davidicus
davidicus / dataURI.sh
Created April 2, 2019 13:58
Create Data URI from image
# https://superuser.com/questions/120796/how-to-encode-base64-via-command-line#comment280484_120815
# create and copy URI to clipboard
openssl base64 < path/to/file.png | tr -d '\n' | pbcopy
# or
cat path/to/file.png | openssl base64 | tr -d '\n' | pbcopy
@davidicus
davidicus / getFileType.js
Created October 24, 2018 01:21
Get files of specific type using node
const promisify = require('util').promisify;
const path = require('path');
// @param: fileType - the file extension you are searching for
// @param: filePath - the path to directory you would like to search. Defaults to __dirname
// @returns: <Promise> which will eventually resolve to an array with either the files names or empty
const getFileType = (fileType, filePath = './') => {
const match = '([a-zA-Z0-9\\s_\\.\\-():])+(';
const regEx = new RegExp(match + fileType + ')$', 'i');
@davidicus
davidicus / rename.js
Created October 23, 2018 02:11
Rename files using regexp and Node.js
var fs = require('fs'),
path = require('path'),
args = process.argv.slice(2),
dir = args[0],
match = RegExp(args[1], 'g'),
replace = args[2],
files;
files = fs.readdirSync(dir);
@davidicus
davidicus / reduce.js
Last active October 3, 2018 14:52
Array Reduce examples and use cases
// Flatten array
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(
( accumulator, currentValue ) => accumulator.concat(currentValue),
[]
);
// flattened is [0, 1, 2, 3, 4, 5]
// Remove duplicate items in an array
@davidicus
davidicus / millisecond.js
Created September 10, 2018 18:31
Millisecond math for common time spans used in timers like setTimeout and setInterval
// Math for common time spans used in timers like setTimeout and setInterval
// Once a minute
// there are 1000 milliseconds in a second and 60 seconds in a minute
1000 * 60
// Once an hour
// Same as above and there are 60 minutes in an hour
1000 * 60 * 60