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 date = new Date(); | |
useDebugValue(date, date => date.toISOString()); |
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 myNumberRef = useRef(0); | |
myNumberRef.current += 1; |
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 MyInput = () => { | |
const inputRef = useRef<HTMLInputElement>(null); | |
return <input ref={inputRef} /> | |
} |
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 MyInput, { MyInputHandles } from './MyInput'; | |
const Autofocus = () => { | |
const myInputRef = useRef<MyInputHandles>(null); | |
useEffect(() => { | |
if (myInputRef.current) { | |
myInputRef.current.focus(); | |
} | |
}); |
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 interface MyInputHandles { | |
focus(): void; | |
} | |
const MyInput: RefForwardingComponent<MyInputHandles, MyInputProps> = ( | |
props, | |
ref | |
) => { | |
const inputRef = useRef<HTMLInputElement>(null); |
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 value = 10; | |
// inferred as number | |
const result = useMemo(() => value * 2, [value]); | |
const multiplier = 2; | |
// inferred as (value: number) => number | |
const multiply = useCallback((value: number) => value * multiplier, [multiplier]); |
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
useEffect(() => { | |
const subscriber = subscribe(options); | |
return () => { | |
unsubscribe(subscriber) | |
}; | |
}, [options]); |
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
type Theme = 'light' | 'dark'; | |
const ThemeContext = createContext<Theme>('dark'); | |
const App = () => ( | |
<ThemeContext.Provider value="dark"> | |
<MyComponent /> | |
</ThemeContext.Provider> | |
) | |
const MyComponent = () => { |
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
interface State { | |
value: number; | |
} | |
type Action = | |
| { type: 'increment' } | |
| { type: 'decrement' } | |
| { type: 'incrementAmount'; amount: number }; | |
const counterReducer = (state: State, action: Action) => { |
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
// inferred as number | |
const [value, setValue] = useState(0); | |
// explicitly setting the types | |
const [value, setValue] = useState<number | undefined>(undefined); | |
const [value, setValue] = useState<Array<number>>([]); | |
interface MyObject { | |
foo: string; | |
bar?: number; |
NewerOlder