最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

solid js - Getting a 'Hydration mismatch' when using splitProps in SolidStartSolidJS - Stack Overflow

programmeradmin4浏览0评论

I'm getting a hydration mismatch error for this code, and I don't really understand why.

Hydration Mismatch. Unable to find DOM nodes for hydration key: 00000000100...
and indicating the child of this component

Here is a minimal code sample:

export function Button(props) {
    const [specialProps, passedProps] = splitProps(props, [
        "class",
        "children",
        "disabled",
        "leftIcon",
    ]);

    const buttonCva = cva("btn", {
        variants: {
            disabled: {
                true: "btn-disabled",
            },
        },
    });

    return (
        <button class={buttonCva(specialProps)} {...passedProps}>
            {specialProps.leftIcon}
            {specialProps.children}
        </button>
    );
}

What I figured out so far is that if I remove "children" from specialProps (so it will be in passedProps), it works.

I'm getting a hydration mismatch error for this code, and I don't really understand why.

Hydration Mismatch. Unable to find DOM nodes for hydration key: 00000000100...
and indicating the child of this component

Here is a minimal code sample:

export function Button(props) {
    const [specialProps, passedProps] = splitProps(props, [
        "class",
        "children",
        "disabled",
        "leftIcon",
    ]);

    const buttonCva = cva("btn", {
        variants: {
            disabled: {
                true: "btn-disabled",
            },
        },
    });

    return (
        <button class={buttonCva(specialProps)} {...passedProps}>
            {specialProps.leftIcon}
            {specialProps.children}
        </button>
    );
}

What I figured out so far is that if I remove "children" from specialProps (so it will be in passedProps), it works.

Share Improve this question asked Feb 15 at 16:52 Gabriel CséfalvayGabriel Cséfalvay 5395 silver badges14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

So I figured out what is the problem: Any elements that are generated during render, are then searched for in the DOM during hydration. In the code above, the children were rendered twice, because

  1. first, the buttonCva() function evaluated them (probably reading the whole specialProps object), and then,
  2. for the second time, {specialProps.children} evaluated them.

No. 2 got inserted into the DOM, but no. 1 just got discarded - so the hydration script on the client side couldn't find it anywhere in the DOM.


So here is the corrected code:

export function Button(props) {
    const [classProps, childrenProps, passedProps] = splitProps(props, [
        "class",
        "disabled",
    ],[
        "children",
        "leftIcon",
    ]);

    const buttonCva = cva("btn", {
        variants: {
            disabled: {
                true: "btn-disabled",
            },
        },
    });

    return (
        <button class={buttonCva(classProps)} {...passedProps}>
            {childrenProps.leftIcon}
            {childrenProps.children}
        </button>
    );
}

In hindsight, it's a silly mistake. But the hydration process and requirements of SolidStart is not that well documented so far.


Here is some reading that helped me:
Hydration error for rendered Elements that aren't inserted in the DOM during server rendering #1977
<Show when={JSX.Element} /> breaks SSR (causes Hydration Errors) #2345

发布评论

评论列表(0)

  1. 暂无评论