1 | import { h, Component } from 'preact'
|
2 | import loadHtml from '../helpers/load-html'
|
3 |
|
4 | class DynamicHtml extends Component {
|
5 | constructor (props) {
|
6 | super(props)
|
7 | this.state = {
|
8 | loading: true,
|
9 | content: null
|
10 | }
|
11 | }
|
12 |
|
13 | componentDidMount () {
|
14 | this.load(this.props.url)
|
15 | }
|
16 |
|
17 | load (url) {
|
18 | loadHtml(url)
|
19 | .then(content => {
|
20 | this.setState({
|
21 | content,
|
22 | loading: false
|
23 | })
|
24 | })
|
25 | }
|
26 |
|
27 | componentShouldUpdate () {
|
28 | return !this.state.loading
|
29 | }
|
30 |
|
31 | componentWillReceiveProps (props) {
|
32 | this.load(props.url)
|
33 | }
|
34 |
|
35 | render () {
|
36 | if (this.state.loading) {
|
37 | return h('div', null, 'Loading...')
|
38 | }
|
39 |
|
40 | return (
|
41 | h('div', {
|
42 | dangerouslySetInnerHTML: { __html: this.state.content }
|
43 | })
|
44 | )
|
45 | }
|
46 | }
|
47 |
|
48 | export default DynamicHtml
|