The output that I was getting is with html tags; hence I used dangerouslySetInnerHTML to avoid that after which I am getting the error "Type '{ contents: Element; }' is not assignable to type 'string'."
My react code is as follows:
import * as React from 'react';
import { RouteComponentProps } from 'react-router';
interface FetchImageDataState {
imageList: ImageData[];
loading: boolean;
}
export class OurTeam extends React.Component<RouteComponentProps<{}>, FetchImageDataState> {
constructor() {
super();
this.state = { imageList: [], loading: true }; //initialize to default
fetch('api/Home/ourTeam')
.then(response => response.json() as Promise<ImageData[]>)
.then(data => {
this.setState({ imageList: data, loading: false });
});
}
public render() {
let contents = this.state.loading ? <p><em>Loading...</em></p> : this.renderImageTable(this.state.imageList);
let body = { contents };
return <div>
<h1>Team</h1>
<div dangerouslySetInnerHTML={{ __html: body }} />
</div>;
}
private renderImageTable(imageList: ImageData[]) {
return <div className='table'>
{imageList.map(x =>
<div key={x.byteData} >{[x]}</div>)
}</div>;
}
}
export class ImageData {
byteData: string = "";
}
Without usage of dangerouslySetInnerHTML the output of the code was desired text with html tags; and also the image tag was displayed instead of image. The detailed error is: ERROR in [at-loader] ./ClientApp/components/OurTeam.tsx:41:18 TS2322: Type '{ dangerouslySetInnerHTML: { __html: { contents: Element; }; }; }' is not assignable to type 'HTMLProps'. Types of property 'dangerouslySetInnerHTML' are incompatible. Type '{ __html: { contents: Element; }; }' is not assignable to type '{ __html: string; } | undefined'. Type '{ __html: { contents: Element; }; }' is not assignable to type '{ __html: string; }'. Types of property '__html' are incompatible. Type '{ contents: Element; }' is not assignable to type 'string'.
I am new to react, please help to resolve the issue.