I have a React class and the render function works fine. The <li>
tag and the internal <a>
tag render.
However, the string of HTML returned by getListItem
does not render. Instead, it shows up as code on the page, as if it had been escaped.
How do I prevent this behavior in React so I can build dynamic HTML when creating components, as in the (attempted) example below?
class ExchangeListItem extends Component {
constructor(props) {
super(props);
this.state = {
};
}
getListItem() {
const retStr = '<a className="nav-link active" href="'+this.props.exchange.url+'">'+this.props.exchange.name + '</a>';
return retStr;
}
render() {
return (
<li>
<a href="https://wwww.google.com">Link</a>
{this.getListItem()}
</li>
);
}
}
At issue here is specifically how React renders something. If it is a string, it will escape the special characters and cause (in this case) HTML to become displayed text. The other post is only asking about how to change innerHTML. In my case, I am not attempting to change an element but trying to get React to render what I intend in the first place.