Function Components and Hooks in TypeScript example
1 | // import useState next to FunctionComponent |
Hooks with TypeScript
- Examples how to use with TS: https://fettblog.eu/typescript-react/hooks/
- TS advanced: https://medium.com/@jrwebdev/react-hooks-in-typescript-88fce7001d0d
- super advanced: https://levelup.gitconnected.com/usetypescript-a-complete-guide-to-react-hooks-and-typescript-db1858d1fb9c
1 | const Counter:FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => { |
with useState
1 | // explicitly setting the types |
with useRef
1 | const inputEl = useRef<HTMLInputElement>(null); |
with useContext
1 | type Theme = 'light' | 'dark'; |
with useReducer
1 | interface State { |