Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between React.HTMLProps<> and React.HTMLAttributes<T>?

I am trying to defined a props interface for my Component and would like it to include all common attributes.

but found out there are two different interface i can extend

interface MyProps extend React.HTMLProps<HTMLElement> and interface MyProps extend React.HTMLAttributes<HTMLElement>

what is the difference? which one should I use? seems like HTMLProps includes HTMLAttributes, does it mean HTMLProps should be a better candidates?

like image 559
jojo Avatar asked Jan 11 '18 00:01

jojo


People also ask

What is IntrinsicAttributes?

IntrinsicAttributes interface can be used to specify extra properties used by the JSX framework which are not generally used by the components' props or arguments - for instance key in React.

What is a react ElementType?

The definition for React. ElementType is basically “any valid HTML element or another React component.” So 'label' can be a valid value for as because it is extends React.

What is ForwardRefExoticComponent?

The call signature of ForwardRefRenderFunction takes a props object, which must include a member children and a ref object as parameters, whereas the call signature of ForwardRefExoticComponent only takes a props object of arbitrary shape as parameter.

How do I run a JSX file in react?

tsx file to: import * as React from "react"; import * as ReactDOM from "react-dom"; import FirstComponent from './components/FirstComponent' ReactDOM. render( <div> <h1>Hello, Welcome to React and TypeScript</h1> <FirstComponent/> </div>, document. getElementById("root") );


1 Answers

HTMLProps contains more stuff than HTMLAttributes. Stuff like ref etc.

I've done the following in the past :

export interface PrimitiveProps extends React.HTMLProps<HTMLDivElement> { };

And it has worked out fine for me 🌹

like image 147
basarat Avatar answered Oct 19 '22 01:10

basarat