Trouble with @types/react

submited by
Style Pass
2021-07-06 20:00:05

The component creates a div element, but there is no such thing as a div in React Native. If you try to render this component, you would get an error. Invariant Violation: View config not found for name div. Make sure to start component names with a capital letter.

React does not provide its own TypeScript type definitions, so the DefinitelyTyped 1 community has stepped up and written them. These types can be installed through @types/react 2.

For the most part, React's types are great, but as demonstrated above, they are not perfect. Why does TypeScript think that a <div> is valid in a React Native component?

Before we dive into React's type definitions, let's do a quick review of React's createElement function, which is what JSX gets compiled to.

React's behavior varies based on what an element's type is. When it is a string, React treats it as a host component, which are the native elements for a rendering environment. In a React DOM application, div and a are host components.

Leave a Comment