Skip to content

Instantly share code, notes, and snippets.

@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(
@ajitid
ajitid / readme.md
Last active September 13, 2021 20:23
React + MobX useObservables

This hook is probably illegal to use. There is a reason why something like this doesn't ship with mobx-react-lite. (See something similar)

Usage

const somestate = useObservables(() =>
  store.getSomeState()
)

const [statex, statey] = useObservables(() =&gt; [store.statex, statey])
@ajitid
ajitid / App.js
Last active March 2, 2024 10:20
Okay-ish way get title and favicon on the browser
import { useState } from "react";
import ky from "ky";
import cheerio from "cheerio";
function App() {
const [url, setUrl] = useState("");
const [title, setTitle] = useState("");
const [faviconUrl, setFaviconUrl] = useState("");
const handleSubmit = async () => {
@ajitid
ajitid / merge-references.ts
Last active August 21, 2021 05:21
Merge references
import type { MutableRefObject } from 'react';
type References<T extends HTMLElement = HTMLElement> =
| MutableRefObject<unknown>
| ((element: T | null) => void)
| undefined
| null;
/**
* Utility function that let's you assign multiple references to a 'ref' prop