This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type Overlap<T, U> = { [K in Extract<keyof T, keyof U>]: U[K] | T[K] }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const [data, setData] = useState(); | |
useEffect(() => { | |
const load = async () => { | |
const r = await asyncFn(userId); | |
setData(r); | |
}; | |
load(); | |
}); | |
useEffect(() => { | |
handleData(data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }>( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; | |
} |
NewerOlder