Force updates through shadow DOM. #3866
-
I have a component that has a shadow DOM and renders children underneath it, similar to the simplified example below. I can use signals to trigger updates in the children, but I would like some help on figuring out how I can make it so the If you uncomment the commented 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
if (host.shadowRoot) return
const shadow = 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>
} The problem also occurs with signals. If |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
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 |
Beta Was this translation helpful? Give feedback.
Answer: Change to the following:
I was early returning when
shadowRoot
already existed, and without that check it was throwing when…