Skip to content

Instantly share code, notes, and snippets.

View theluk's full-sized avatar

Lukas Klinzing theluk

  • Vienna, Austria
View GitHub Profile
#!/bin/bash
trap "exit 1" ERR
isPush=false
if [[ "$1" == "--push" ]]
then
isPush=true
shift
else
isPush=false
@theluk
theluk / readme.md
Created July 11, 2021 12:42
useMountedEffect.ts

Asynchronous methods within hooks can lead to issues, where your asynchronous code resolves after your component / hook was unmounted.

This usually causes react warnings, that you cannot set state on an unmounted component.

How to use useMountedEffect

const MyComponent = ({ id }) => {
 const [status, setStatus] = useState();
@theluk
theluk / useLocal.md
Last active July 11, 2021 12:30
useLocal: like useState, but updates if initial value changes

In situations where you initialize useState with some value and later on you want to update the state, because the parent changed, you would need to currently listen on the parent change in an useeffect and then set the state to update it, or more precisely, update the local version.

For this a hook I use quite often for forms or temporarily updated values is the useLocal hook.

Let's take this example

const RangeDatepicker = ({ value: { from, to }, onChange }) => {
@theluk
theluk / loader.js
Last active December 26, 2015 10:19
we are migrating, just a few tips for our content writers.
(function(){
var ht = document.createElement('script');
ht.type='text/javascript';
ht.src = 'https://gist.github.com/theluk/7135416/raw/d33a0bde05eeab38ebbfedf09328a8d07d4123e5/main.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ht, s);
})();