I am very new to React and TypeScript. I am developing a web page in Visual Studio 2017 where I have a search component which I would like to use in my home component. But after importing search into my home component, I get a compile time error saying:
Type {} is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Search> & Readonly<{ children?: ReactNode; }>
Type {} is not assignable to type Readonly<SearchForm>
Property 'suggestionList' is missing in type {}
Search Component code:
export class Search extends React.Component<SearchForm> { //SearchForm is a class with string and an object of another class as properties
constructor() {
super();
}
Home component code:
export class Home extends React.Component<RouteComponentProps<{}>, {}>
{
return <div>
< Search /> // <=Getting error at this line
<div>
}
If I remove RouteComponentProps<{}>
from Home component, then I get an error in route.tsx file. I know, I must be doing something wrong or some silly mistake. I googled this but I am not getting any helpful answers. Can somebody tell me how do I solve this?
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.
js error "Property does not exist on type 'Readonly<{}>'" occurs when we try to access the props or state of a class component which we haven't typed. To solve the error, use the generic on the React. Component class to type the props or state objects of the class. Here is an example of how the error occurs.
By invoking them between the opening and closing tags of a JSX element, you can use React children for entering data into a component. The React children prop is an important concept for creating reusable components because it allows components to be constructed together.
As @Tobías said, you are missing the required properties of Search component.
Giving all props a value will fix it:
<Search prop1={'string'} prop2={obj} />
If you would like to create functional component you can use generics in order to get rid of this error. Consider this example.
interface SearchProps {
suggestionList: any
}
export const Search: React.FC<SearchProps> = ({suggestionList}): JSX.Element => {
return <div>...</div>
}
You are missing required properties of Search
component, the error shows suggestion List
. You need something like:
<div>
<Search suggestionList={...} />
</div>
(and maybe not only suggestionList
is missing)
React.Component definition is:
interface Component<P = {}, S = {}>
where P
defines components properties and S
the state. You must pass to the component all requiered properties defined in P
.
In your example, component properties type is SearchForm
. Therefore you need to pass, at least, all required attributes of SearchForm
.
Please refer to Define Props and State Types for a more complete explanation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With