Skip to content

Instantly share code, notes, and snippets.

View finom's full-sized avatar
🇺🇦
Slava Ukraini!

Andrii Gubanov finom

🇺🇦
Slava Ukraini!
View GitHub Profile
@finom
finom / createComponentPortal.tsx
Created October 3, 2023 11:49
Allows to create portals within react components
import React, {
createContext,
useState,
useContext,
ReactNode,
FunctionComponent,
Dispatch,
SetStateAction,
} from 'react';
@finom
finom / authGuard.ts
Last active June 20, 2023 11:23
A beautiful way to add NestJS decorator vibes for NextJS app API route to check for authorisation or anything else
import { NextRequest, NextResponse } from 'next/server';
import { checkAuth } from './checkAuth'; // define it by yourself
const httpMethods = ['GET', 'POST', 'PUT', 'DELETE'] as const;
type MethodMap<T> = { GET?: T; POST?: T; PUT?: T; DELETE?: T };
export default function authGuard(options?: MethodMap<boolean>) {
return function AuthGuard(given: Function) {
const ctr = given as Omit<Function, 'prototype'> & {
import { useEffect, useRef } from 'react';
/**
*
* @example
* const ref = useClickOutside<HTMLDivElement>((evt: MouseEvent) => console.log('blah'))
* <div ref={ref} />
*/
type ClickEvent = MouseEvent & { target: HTMLElement };
@finom
finom / QuerySelector.ts
Created October 5, 2022 18:15
QuerySelector react element
import { useEffect } from 'react';
interface Props {
selector: string;
className?: string;
onClick: (evt: Event) => void;
}
const QuerySelector = ({
selector,
@finom
finom / Touchable.js
Last active December 16, 2020 17:09
[finom/react-native-touchable] A universal Touchable (React Native) which behaves like TouchableOpacity on iOS but like TouchableNativeFeedback on Android
import { TouchableOpacity, TouchableNativeFeedback, Platform } from 'react-native';
import { createElement } from 'react';
// use TouchableNativeFeedback for android and TouchableOpacity for ios
// use TouchableOpacity with activeOpacity=1 for both platforms if disabled=true
const Touchable = ({
disabled,
onPress,
...props
}) => {
@finom
finom / useArrayState.js
Last active June 29, 2020 18:32
[finom/use-array-state] A hook for creating observable-like immutable arrays based on useState
/*
A hook for creating observable-like immutable arrays based on useState
in other words allows to call push, pull, slice... methods which are going to deliver
a new array every time and trigger component to re-render
Usage:
const [array, arrayRef] = useArrayState(initialValue = []);
arrayRef.push(foo); // triggers component to re-render and makes 'array' to update
@finom
finom / useBooleanState.js
Last active June 29, 2020 12:38
[finom/use-boolean-state] Simplifies boolean useState creation
/*
Usage:
const [value, setTrue, setFalse] = useBooleanState(initialValue);
*/
import { useState, useCallback } from "react";
export default function useBooleanState(initialValue) {
const [value, onSetValue] = useState(initialValue);
const onSetTrue = useCallback(() => onSetValue(true), []);
@finom
finom / addeventlistener.js
Created August 22, 2014 13:32
Simple addEventListener polyfill for IE8
( function( win, doc, s_add, s_rem ) {
if( doc[s_add] ) return;
Element.prototype[ s_add ] = win[ s_add ] = doc[ s_add ] = function( on, fn, self ) {
return (self = this).attachEvent( 'on' + on, function(e){
var e = e || win.event;
e.target = e.target || e.srcElement;
e.preventDefault = e.preventDefault || function(){e.returnValue = false};
e.stopPropagation = e.stopPropagation || function(){e.cancelBubble = true};
e.which = e.button ? ( e.button === 2 ? 3 : e.button === 4 ? 2 : e.button ) : e.keyCode;
fn.call(self, e);
@finom
finom / createStateContext.js
Created May 11, 2020 14:08
[finom/create-state-context] Creates useState-like hook which uses react context
/*
Can be used to achieve useState hook experience with React Context API
1. Create context with some initial state:
const MyContext = createStateContext(0);
2. Wrap your component by the context provider
<MyContext.Provider>
<MyComponent />
</MyContext.Provider>
3. Use it in your components with built-in react useContext hook:
import { useContext } from 'react';
@finom
finom / useForceUpdate.js
Created May 11, 2020 13:48
[finom/use-force-update] Allows to make force re-render of react components
/*
Usage:
const [forceIndex, forceUpdate] = useForceUpdate();
// call forceUpdate() when component needs to be updated
// use forceIndex as key to force re-render child components
Important! This function is most likely an antipattern! But sometimes I can be super useful:
it can re-render react native screens when something gone wrong and user is prompted by a "Try again" button.
*/