1 6. bind:this
~coolelectronics edited this page 2024-03-10 13:52:15 -04:00

bind:this

As an alternative to doing this.root.querySelector(...), bind:this lets you pick out a specific element in the component and assign it to a variable.

In this example we create a sacrificial <div>, assign it to this.renderRoot, and then after mount we can access its innerHTML property as well as any other.

const RawHTMLRenderer: Component<{
	html: string
}, {
	renderRoot: HTMLDivElement
}> = function() {
	this.mount = () => {
		this.renderRoot.innerHTML = this.html
	}
	return (
		<div>
			unsanitized html below:
			<div bind:this={use(this.renderRoot)} />
		</div>
	)
}

document.body.appendChild(<RawHTMLRenderer html="<h1>hi</h1>" />)