I have some HTML as a String that I want to displa...
# compose-web
r
I have some HTML as a String that I want to display in a
Div
. Do we have something like React's
__dangerouslySetInnerHtml
function?
This works but ouch:
Copy code
DomSideEffect {
  asDynamic()._elementScope._element_3.innerHTML = someHtml
}
o
https://github.com/JetBrains/compose-jb/tree/master/tutorials/Web/Using_Effects#disposablerefeffect DisposableRefEffect should be better because you can pass
key
, so the effect will be invoked only when yourHtmlString changes
Copy code
Div {
    
    DisposableRefEffect(key = yourHtmlString) { htmlDivElement ->
        htmlDivElement.innerHtml = yourHtmlString
        onDispose {
           htmlDivElement.innerHtml = ""
        }
    }
}
r
Thank you for the example and link to the docs, I knew one of the effects was the right approach but couldn't find the right one.
b
Even simpler is using
prop
inside the AttrsBuilder callback, e.g:
Copy code
Div({
  prop(HTMLElement::innerHTML::set, someHtml)
})
The lambda on the left side will be called whenever the value on the right changes
👍 2
r
That works @Brian Guertin, thanks