I want to separate JavaScript and HTML in react. I'm not a big fan of their philosophy of mixing them together.
So, basically I created my component .js
files. For example teachersList.js
and this is my code:
class Teacher extends React.Component {
constructor() {
super();
this.state = { teachersCount: 20, html: <div>loading</div> }
}
componentDidMount() {
var that = this;
setTimeout(function () {
that.setState({ html: <div> dynamic HTML is loaded: {that.state.teachersCount}</div> })
}, 2000);
}
render() {
return (
<div >
<div dangerouslySetInnerHTML={{ __html: this.state.html }}></div>
</div>
)
}
}
And it works just fine. However, when I change the componentDidMount
function to load HTML from server, it breaks:
componentDidMount() {
var that = this;
$.get("/teacher/list.html", function (data) {
that.setState({ html: data })
})
}
What should I do?
question from:https://stackoverflow.com/questions/65842810/how-to-make-react-know-my-separately-loaded-html