Preact: dangerouslySetInnerHTML?

Created on 28 Jan 2016  路  6Comments  路  Source: preactjs/preact

In React, you can set inner HTML with something like:

<div dangerouslySetInnerHTML={{__html: 'my markup'}} />

but preact doesn't seem to support this? Any tips on how to achieve something similar?

enhancement

Most helpful comment

You, sir, are a scholar and a gentleman. :)

All 6 comments

This is intentionally not in preact, though it could be added to preact-compat.
You can set innerHTML in a life cycle method like componentDidUpdate or componentDidMount.

Example:

class Foo extends Component {
  componentDidMount() {
    this.base.innerHTML = 'hello';
  }
}

That works client side, but obviously would not work for server side rendering (since componentDidMount() will never run on the server.)

(Apologies if this comment shows up twice - I replied by email but github seems to have lost the email version. :P)

True, very good point. Perhaps that's the selling point of __dangerouslySetInnerHTML then!

@jwalton - just published a prerelease of 3.0 which supports dangerouslySetInnerHTML. On npm as preact-3.0.0-beta1

You, sir, are a scholar and a gentleman. :)

Now generally available as of 3.2.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matuscongrady picture matuscongrady  路  3Comments

jasongerbes picture jasongerbes  路  3Comments

skaraman picture skaraman  路  3Comments

matthewmueller picture matthewmueller  路  3Comments

simonjoom picture simonjoom  路  3Comments