Skip to content

Force updates through shadow DOM. #3866

Answered by MicahZoltu
MicahZoltu asked this question in Q&A
Discussion options

You must be logged in to vote

Answer: Change to the following:

import { render } from "preact";
import { useReducer } from "preact/hooks";

export function Apple() {
	const [, forceUpdate] = useReducer<number, undefined>(x => x + 1, 0)
	const input = <input key='1' value={Math.random()}/>
	const shadowHost = <span ref={host => {
		if (host === null) return
		const shadow = host.shadowRoot || host.attachShadow({mode: "open"})
		render(input, shadow) // this does not work
		// render(input, host) // this works
	}}/>
	return <div>
		<button onClick={() => forceUpdate(undefined)}>Force Update</button>
		{shadowHost}
	</div>
}

I was early returning when shadowRoot already existed, and without that check it was throwing when

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by MicahZoltu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant