score:310
Yeah, this is a quirk of how the typings are written:
function useRef<T>(initialValue: T): MutableRefObject<T>;
function useRef<T>(initialValue: T|null): RefObject<T>;
If the initial value includes null
, but the specified type param doesn't, it'll be treated as an immutable RefObject
.
When you do useRef<HTMLInputElement>(null)
, you're hitting that case, since T
is specified as HTMLInputElement
, and null
is inferred as HTMLInputElement | null
.
You can fix this by doing:
useRef<HTMLInputElement | null>(null)
Then T
is HTMLInputElement | null
, which matches the type of the first argument, so you hit the first override and get a mutable ref instead.
score:5
I came to this question by searching how to type useRef
with Typescript when used with setTimeout
or setInterval
. The accepted answer helped me solve that.
You can declare your timeout/interval like this
const myTimeout = useRef<ReturnType<typeof setTimeout> | null>(null)
And to clear it and set it again, you do it as usual:
const handleChange = () => {
if (myTimeout.current) {
clearTimeout(myTimeout.current)
}
myTimeout.current = setTimeout(() => {
doSomething()
}, 500)
}
The typing will work both if you're running in Node or in a Browser.
Source: stackoverflow.com
Related Query
- What typescript type do I use with useRef() hook when setting current manually?
- When setting state with React's use state hook using it's prev (or initial) value throws an error Type Error 0 is not a function
- Property 'value' does not exist on type 'never'. when use useRef hook in mui
- How to use AutoPlay methods with React-slick useRef hook and typescript
- What Type should I use for TypeScript props with children?
- Typescript error when use react useState hook with generics
- How to type functions in useRef hook with TypeScript
- What does type Props mean in react and typescript when used with Omit?
- What type to use when implement RouteComponentProps on react typescript component
- What TypeScript type should I use to reference the match object in my props?
- How to use React useRef hook with typescript?
- React hook useRef not working with styled-components and typescript
- Type Error when using Typescript with React-Redux
- How do you set the Typescript type for useRef hook in React Native?
- How to use useRef hook to select next TextInput after pressing next keyboard button with redux-form
- When to use private/protected methods in Typescript with React
- How do you correctly use React.lazy() in Typescript to import a react component with a generic type parameter?
- FlatList scrollToIndex using useRef with Typescript - Type Error
- React Custom Hook with Typescript Type error "Property 'x' does not exist on type 'interface | (({ target }: any) => void)'.ts(2339)"
- What type should I use for input react-bootstrap event with TypeScript?
- How to use radium in React typescript (Error: has no properties in common with type 'CSSProperties'. TS2559)?
- How to use React Hook Form with Typescript (Input Component)
- Mobx - when to use useLocalStore hook with react-mobx
- Typescript - Setting the type for a null property, when defining the initial state in Redux
- Typescript - A function which returns a function with generic type - what does it do?
- How to use useState hook in React with typescript correctly?
- How to use React.forwardRef with TypeScript when using different html elements in the render
- How do I fix a React TypeScript error with the return value of useHotkeys hook not matching the type of the div element ref prop
- What type should I use for a React component with arbitrary properties?
- How to use custom hook (fetch) with typescript
More Query from same tag
- fontFamily "$text-font-family" is not a system font and has not been loaded through Font.loadAsync
- Open new tab with useNavigate hook in React
- React change content based on item selected in Dropdown
- React context in MPA (one context for different apps)
- How to create a controlled input with empty default in React 15
- How to change from one text field to another
- Install babel-plugin-styled-components in create-react-app
- How would I add stack tracing to my current Redux Devtools setup?
- Download pdf from url using html
- Spring Boot (inject script in reactjs)
- Can we Schedule deployments in vercel Deploy Hooks?(Next.js)
- Change fill of svg on hover
- ReactJS - Handling onClick events for an entire section except for a few child elements
- React search onChange cancel request
- How to get "key" prop from React element (on change)?
- coreui custom components and mobile version logo
- Onclick button is not passing props to the functional component React
- No line break when passing value containing "\n" received from server side to textarea
- React Redux Typescript error with AnyAction when implementing Redux-Persist
- How to rename react-native entry file (index.ios.js)
- Firebase error while using firestore in NextJS
- React Query execute mutation after query is fetched
- how to synchronize multiple d3 charts when zoomed?
- How to get Ant Design Form data in another Component
- Put cursor inside input box onChange in React
- react-crop-image error in converting canvas to blob
- React Carousel / Image Slider
- Reactjs : Append instead of replacing with render method
- Set value in Select (Input) of reactstrap
- Check if min-width is set