score:9
Accepted answer
To set the type for the ref, you set the type like this: useRef<HTMLVideoElement>()
. Then, to handle the fact that the object is possibly null
(since it's null or undefined before the component is mounted!), you can just check whether it exists.
const App = () => {
const video = useRef<HTMLVideoElement>();
const playVideo = (event: any) => {
video.current && video.current.play();
};
return (
<div className="App">
<video ref={video} loop src={bike} />
</div>
);
};
Source: stackoverflow.com
Related Query
- How can I define the type for a <video> reference in React using Typescript?
- How do I restrict the type of React Children in TypeScript, using the newly added support in TypeScript 2.3?
- How can I define TypeScript type for a setState function when React.Dispatch<React.SetStateAction<string>> not accepted?
- How do you set the Typescript type for useRef hook in React Native?
- can i use the tsx extension for test files if using react with typescript
- How do I define a TypeScript type for React props where prop B is only accepted if prop A is passed to a component?
- How to properly declare type for React wrapper component (HOC) using Typescript
- In React Typescript how to define type for ...rest parameter for native html elements?
- How to correctly type a null value in typescript react app, retrieved from an JSON object (api call) when using Object.entries to retrieve the value?
- how can my call the action in my component using typescript with react redux?
- How to setup a generic interface for the state when using React useReducer with typescript
- How can I properly define a type interface when using React useContext?
- How to define TypeScript type for object wrapper that has string as a key & React SFC as a value?
- How can I show the latest data by date using sort in react typescript
- How to type the initial state for react context when using useState as value
- How can I define the route for the following schema in react router dom
- How to define the return type of React FunctionalComponent in TypeScript definition
- Typescript ESLint Error - How can I type children when using react functional components?
- How can I use different properties for the same component using Styled Component for React JS?
- In my rest webservice request for react using axios, the response json contains html div element. how can I get the value of this element?
- How can I test a change handler for a file-type input in React using Jest/Enzyme?
- How can I write an else if structure using React (JSX) - the ternary is not expressive enough
- How do I create a TypeScript type definition for a stateless React component?
- How can remove console.log in the production build of a React application created using create-react-app?
- How can i create a table using the react-pdf library for generation pdf report?
- How to use typescript with the definition of custom styles for a select using react-select
- CDN links for React packages and how to import it when using react using the scripts from CDN
- how to type state and dispatch for useReducer - typescript and react
- what is the correct type for a React instance of a component in typescript
- How can I change the format for react datepicker days of the week?
More Query from same tag
- Fable Bindings for react-beautiful-dnd throwing error building Droppable
- Exclude node_module/package from production build webpack
- Does ReactDOM.render wait for the document to be loaded before rendering?
- Cannot read property 'map' of undefined when destructing props
- How to filter array from a stacked array in react
- react router v4 link onclick get link prop
- Why is my redux store mapping properties to an object with a single property with the value?
- Firebase: TypeError: Cannot read properties of undefined (reading 'getIdToken')
- What is the difference between (a: ?string) and (a?: string) in flow type?
- React Context API is slow
- Getting ERR! missing script: start on <npm start> command
- How do I add Constructor in create-react-app?
- Feed multiple parameters from node to mssql using node-mssql and stored procedures
- How to reformat date before displaying in Table
- ReactJs: How to map an object
- React Router - passing Slug as a prop to child Component and not getting params of url
- Add dynamic texture to model using three js and React Three Fiber and gltfjsx
- How to disable Submit button outside the Antd Form when the fields are invalid?
- How to create moving image from right to left with reverse using react-spring?
- React Typescript Generic Type Overloading (e.g. useState)
- Hash router works in production but browser router doesn't in react webpack app
- Group objects by one of its params with javascript
- Objects are not valid as a React child when trying to map data from my api
- In a MERN app, if you have a app.get('/') on your server and a <Route path=/' > on your client, which one will trigger first or take priority?
- How to make MUI Fade fade in on render?
- Duplicate YouTube iframe embed video playback
- How do you add an annotation to a website without altering the layout?
- Removing sub component from React component?
- React Hooks - Best way to remove an component temporarily from DOM without unmounting it
- Is it possible to get value right after api call?