Skip to content

Instantly share code, notes, and snippets.

View treyhuffine's full-sized avatar

Trey Huffine treyhuffine

View GitHub Profile
const refContainer = useRef(initialValue);
const computeExpensiveValue = (end: number) => {
let result = 0;
for (let i = 0; i < end * 1000000; i++) {
for (let j = 0; i < end * 1000; j++) {
result = result + i - j;
}
}
return result;
function useMemo<T>(factory: () => T, deps: DependencyList): T;
const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);
function useCallback<T extends (...args: any[]) => any>(callback: T, deps: DependencyList): T;
const memoizedCallback = useCallback(
() => {
doSomething(a, b);
},
[a, b],
);
const [state, dispatch] = useReducer(reducer, initialState, init);
import { createContext, useContext } from 'react';
props ITheme {
backgroundColor: string;
color: string;
}
// The standard way to create context. It takes an initial value object
const ThemeContext = createContext<ITheme>({
backgroundColor: 'black',
color: 'white',
})
function useEffect(effect: EffectCallback, deps?: DependencyList): void;
// The first argument, `effect`
type EffectCallback = () => (void | (() => void | undefined));
// The second argument, `deps?`
type DependencyList = ReadonlyArray<any>;
useEffect(() => {
const subscription = props.source.beginAPIPoll();
return () => {
// Clean up the subscription
subscription.cancelAPIPoll();
};
});