我有一个React组件返回一个HTML表格。React组件返回原始HTML
调用使用:<Options list={item} />
这是返回表中的功能组件:
const Options = (props) => {
let table = `
<table className="table table-striped table-hover ">
<thead>
<tr>
<th>#</th>
<th>Option</th>
<th>Votes</th>
</tr>
</thead>
<tbody>
`
for (let i = 0; i < props.list.options.length; i++){
table += `<tr>
<td>${i+1}</td>
<td>${props.list.options[i].option}</td>
<td>${props.list.options[i].vote}</td>
</tr>
`
}
table += `</tbody></table>`
return table;
}
但我在屏幕上看到的是:
怎么来的HTML没有被浏览器渲染?
这是因为你实际上是返回一个字符串。 –
我鼓励你[学习JSX](https://reactjs.org/docs/jsx-in-depth.html),和一个HTML字符串的区别,这就是你现在使用的。 –