Skip to content

Instantly share code, notes, and snippets.

View bpander's full-sized avatar

Brad Anderson bpander

  • Ann Arbor
View GitHub Profile
export type Overlap<T, U> = { [K in Extract<keyof T, keyof U>]: U[K] | T[K] };
import { useEffect, useRef } from 'react';
export const usePreviousDifferent = <T>(value?: T): T | undefined => {
const shortTermRef = useRef<T | undefined>(value);
const longTermRef = useRef<T>();
useEffect(() => {
shortTermRef.current = value;
return () => { longTermRef.current = value; };
}, [value]);
const [data, setData] = useState();
useEffect(() => {
const load = async () => {
const r = await asyncFn(userId);
setData(r);
};
load();
});
useEffect(() => {
handleData(data);
import _get from 'lodash/get';
import { setIn } from './objects';
// This is a lens utility based off of https://github.com/utatti/lens.ts with 3 main differences.
// 1. It keeps track of the path from the source to the destination
// 2. It works by feeding that path into lodash's get/setWith functions
// 3. It doesn't use proxies (so IE is supported)
export type Setter<T> = (state: T) => T;
import { createReducer } from '@sws/lib/createReducer';
import { ThunkAction } from 'redux-thunk';
import { AnyAction } from 'redux';
// ThingService.ts
interface Thing { id: string; name: string }
class ThingService {
static async fetch(thingId: string) {
/* api call */
import 'redux';
type Duck<T> = {
reducer: (state: T) => T;
};
type GetBranch<T> = <R>(rootState: R) => T;
type DuckCreator<T> = (getBranch: GetBranch<T>) => Duck<T>;
interface DuckCreatorMap {
[key: string]: DuckCreator<any>;
}
import { Reducer, AnyAction, Dispatch } from 'redux';
import { PartialRecord } from '@src/lib/PartialRecord';
export interface Action<T, TPayload> {
type: T;
payload: TPayload;
}
export type ActionConfig<T, P> = (payload: P) => Action<T, P>;
import { configureActionsWith } from './configured-actions';
interface CounterState { count: number }
const initialState: CounterState = { count: 0 };
const { configureAction, reducer } = configureActionsWith(initialState, 'COUNTER');
export const counterReducer = reducer;
export const addAction = configureAction<{ amt: number }>(
import { Reducer, Action } from 'redux';
interface CounterState { count: number }
const initialState: CounterState = { count: 0 };
const ADD = 'ADD';
interface AddAction extends Action<typeof ADD> {
payload: { amt: number };
}
import { get, set } from 'lodash';
type InnerA = { type: 'num'; amount: number; }
type InnerB = { type: 'str'; name: string; }
type Inner = InnerA | InnerB;
interface Outer {
inners: Inner[];
}