Skip to content

Instantly share code, notes, and snippets.

View donaldpipowitch's full-sized avatar

Donald Pipowitch donaldpipowitch

View GitHub Profile
@donaldpipowitch
donaldpipowitch / use-stable-memo.ts
Last active October 15, 2020 08:12
useStableMemo
import { DependencyList, useState, useEffect, useRef } from 'react';
// use this instead of useMemo of you need a stable value
// see https://twitter.com/0xca0a/status/1314326386555924480
export function useStableMemo<T>(factory: () => T, deps: DependencyList): T {
const [value, setValue] = useState<T>(factory);
const firstRun = useRef(true);
useEffect(
() => {
@donaldpipowitch
donaldpipowitch / mocked-storage.tsx
Last active July 22, 2020 13:10
useSessionStorage and useLocalStorage for easy mocking - similar to MemoryRouter in react-router, but for storage
import React, { FC, useMemo } from 'react';
import {
LocalStorageProvider,
SessionStorageProvider,
} from './use-storage';
// this is just a dumb mock of the storage interface
class MemoryStorage implements Storage {
private data: Record<string, string> = {};
@donaldpipowitch
donaldpipowitch / use-location-state.ts
Created July 22, 2020 06:23
useLocationState: a glorified location.state for React Router as a hook
import { useState, useEffect } from 'react';
import { useLocation, useHistory } from 'react-router-dom';
import { useSessionStorage } from './use-storage'; // see https://gist.github.com/donaldpipowitch/7310d7b9e4b6d467134c425e8732adc6
type UseLocationStateOptions<T> = {
defaultValue?: T;
scope?: string;
filter?: (value: T) => boolean;
};
@donaldpipowitch
donaldpipowitch / index.js
Created July 7, 2020 07:38
ESLint rule to check for round brackets in Jest `test` names. (Doesn't play nicely with --testNamePattern without escaping.)
// @ts-check
/** @type {import("eslint").Rule.RuleModule} */
const rule = {
meta: {
docs: {
description: `If you use a Jest test name like "hello (world)" you can't run \`$ jest -t "hello (world)"\` to select this test.`,
},
fixable: 'code',
},
@donaldpipowitch
donaldpipowitch / index.js
Last active July 7, 2020 07:36
ESLint rule to check for duplicated exported names.
// @ts-check
/** @type {import("eslint").Rule.RuleModule} */
const rule = {
meta: {
docs: {
description: `You can export a type and a value with the same name in the same file, but that confuses refactoring tools.`,
},
},
create(context) {
@donaldpipowitch
donaldpipowitch / README.md
Created July 1, 2020 13:17
Cheat Sheet for various Brackets

Know your brackets!

  ( ) – parentheses, round brackets, first brackets
  { } – braces, curly brackets, second brackets, moustache brackets 👨🏻
  [ ] – square brackets, third brackets
  ⟨ ⟩ - angle brackets (aka. _not_ less-than/greater-than signs 👀)
  ‹ › - angle quotes (aka. _not_ less-than/greater-than signs 👀)
  < > - less-than/greater-than signs
@donaldpipowitch
donaldpipowitch / index.tsx
Created June 27, 2020 19:46
Example how to use 'amazon-cognito-identity-js'
import {
CognitoUserPool,
CognitoUserAttribute,
CognitoUser,
AuthenticationDetails,
CognitoUserSession,
CodeDeliveryDetails,
} from 'amazon-cognito-identity-js';
import React, { FC, useState } from 'react';
@donaldpipowitch
donaldpipowitch / README.md
Created October 8, 2019 06:09
A React hook to create Axios Mocks

A simple hook which can be used to create Axios mock. The hook will throw an error if an unmocked route was found.

Works great in Storybook stories:

export const EmptyResult = () => {
  useMock((mock) => {
    const result = [];
    mock.onGet(searchUrl).reply(200, result);
 });
@donaldpipowitch
donaldpipowitch / data.json
Last active October 4, 2019 21:25
Country code to Country name in JSON
// from https://en.wikipedia.org/wiki/ISO_3166-2#cite_note-iso31661-2, 4th Oct. 2019
[
{
"code": "AD",
"name": "Andorra"
},
{
"code": "AE",
"name": "United Arab Emirates"
},
@donaldpipowitch
donaldpipowitch / use-touched-handler.tsx
Created October 1, 2019 13:43
Change touch behavior of Formik
import { useState, useEffect } from 'react';
import { useFormikContext, FieldMetaProps } from 'formik';
export function useTouchedHandler(name: string, meta: FieldMetaProps<any>) {
const { setFieldTouched } = useFormikContext<any>();
// use meta.touched as the initial value to keep the state,
// in case this field was unmounted
const [wasChanged, setWasChanged] = useState(meta.touched);