I have an array of objects with name
property that has a string with h1
element tag. When I tried to bind data to a table td it shows as plain text instead of html.
Example reproduced here: https://stackblitz.com/edit/react-table-example-2rcaug?file=index.js
class App extends React.Component {
constructor() {
this.state = {
data:
[
{
"id": 1,
"name": "<h1>Foo</h1>",
"age": "20"
},
{
"id": 2,
"name": "<h1>Bar</h1>",
"age": "30"
},
{
"id": 3,
"name": "<h1>test</h1>",
"age": "40"
}
]
}
}
componentDidMount(){
let test = [...this.state.data]
test.map((col, i) => {
var wrapper = document.createElement("div");
wrapper = col.name;
test[i].name = wrapper
console.log(wrapper)
});
this.setState({data : test})
}
render() {
return (
<div>
<Header />
<table>
<tbody>
{this.state.data.map((person, i) => <TableRow key={i} data={person}
/>)}
</tbody>
</table>
</div>
);
}
}
class Header extends React.Component {
render() {
return (
<div>
<h1>Header</h1>
</div>
);
}
}
class TableRow extends React.Component {
render() {
return (
<tr>
<td>{this.props.data.id}</td>
<td>{this.props.data.name}</td>
<td>{this.props.data.age}</td>
</tr>
);
}
}`