This file contains hidden or 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 refContainer = useRef(initialValue); |
This file contains hidden or 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 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; |
This file contains hidden or 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
function useMemo<T>(factory: () => T, deps: DependencyList): T; |
This file contains hidden or 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 memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]); |
This file contains hidden or 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
function useCallback<T extends (...args: any[]) => any>(callback: T, deps: DependencyList): T; |
This file contains hidden or 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 memoizedCallback = useCallback( | |
() => { | |
doSomething(a, b); | |
}, | |
[a, b], | |
); |
This file contains hidden or 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 [state, dispatch] = useReducer(reducer, initialState, init); |
This file contains hidden or 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 { 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', | |
}) |
This file contains hidden or 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
function useEffect(effect: EffectCallback, deps?: DependencyList): void; | |
// The first argument, `effect` | |
type EffectCallback = () => (void | (() => void | undefined)); | |
// The second argument, `deps?` | |
type DependencyList = ReadonlyArray<any>; |
This file contains hidden or 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
useEffect(() => { | |
const subscription = props.source.beginAPIPoll(); | |
return () => { | |
// Clean up the subscription | |
subscription.cancelAPIPoll(); | |
}; | |
}); |