Skip to content

Instantly share code, notes, and snippets.

@ajitid
ajitid / readme.md
Last active May 17, 2022 12:12
`useEffectOnce` for React v18 (TypeScript)

Inspired from this blog post. If you want to change return type from void to unknown, be my guest.

@ajitid
ajitid / another.js
Last active May 14, 2022 14:58
sum(1)(2)(3)() - sum curry recursive like
function sum(a) {
if(a == null) return 0
return b => b == null ? a : sum(a + b)
}
/*
sum() // 0
sum(1) // 1
sum(1)(2)() // 3
@ajitid
ajitid / Button.tsx
Created April 26, 2022 05:44
React extend HTML element
import React, { forwardRef } from 'react';
import cn from 'classnames';
import css from './input.module.scss';
interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {}
const Input = forwardRef<HTMLInputElement, InputProps>(
({ type = 'text', className, ...props }, ref) => {
let classes = cn(css.input, className);
@ajitid
ajitid / instructions.md
Last active May 27, 2022 10:00
Setup Fedora on WSL
@ajitid
ajitid / a.py
Last active March 31, 2022 18:28
print("Clap along if you feel like that's what you wanna do")
call plug#begin()
Plug 'theHamsta/nvim-treesitter', {'branch': 'ecma-auto-comment', 'do': ':TSUpdate'}
call plug#end()
set fillchars=eob:\ ,
set shm+=Ic
set noswapfile
set lazyredraw
set modelines=0
set number relativenumber
@ajitid
ajitid / debounce.ts
Last active August 24, 2024 17:40
Debounce
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function debounce<F extends (...arguments_: any) => any>(
function_: F,
wait = 200
): F {
let timeoutID: number;
return function (this: unknown, ...arguments_: Parameters<F>) {
clearTimeout(timeoutID);
@ajitid
ajitid / portal.tsx
Created September 15, 2021 21:58
React portal
import type { ReactNode } from 'react';
import { useEffect, useRef } from 'react';
import ReactDOM from 'react-dom';
interface Props {
rootId: string;
children: ReactNode;
}
export const Portal = ({ rootId, children }: Immutable<Props>) => {
@ajitid
ajitid / usage-form.tsx
Last active September 15, 2021 21:55
useAsync + ky
import React, { useCallback, useState } from 'react'
import { useForm } from 'react-hook-form'
import useSignal from 'utils/hooks/useSignal'
import useAsync, { AsyncState } from 'utils/hooks/useAsync'
const UpdateUserForm = () => {
const signal = useSignal()
const { getValues, /* ... */ reset: resetForm } = useForm<FormData>({
@ajitid
ajitid / usage.tsx
Last active September 14, 2021 05:18
Control props — state management for controllable components
interface Props {
value?: string;
onChange?: (value: string) => void;
}
export function OutboundSelect<T>({
value: externalValue,
onChange: externalOnChange = noop,
}: Props) {
const [value, onChange, isInternalValueUsed] = useAutoControlled(