Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug?]: Hydration error when accessing element attribute from props #1752

Closed
2 tasks done
gar1t opened this issue Feb 1, 2025 · 1 comment
Closed
2 tasks done

[Bug?]: Hydration error when accessing element attribute from props #1752

gar1t opened this issue Feb 1, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@gar1t
Copy link

gar1t commented Feb 1, 2025

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

This issue falls under the general banner of this error message:

Hydration Mismatch. Unable to find DOM nodes for hydration key

Image

There are many causes of this error message but this report is specific to accessing an element attribute of props. For example, props.children && ..., if (props.children) { ... }, etc.

I've created a minimal case to reproduce it in this repo:

https://github.com/gar1t/reproduce-solid-hydration-error

This is the offending code:

import { JSXElement } from "solid-js";

function Foo(props: { children?: JSXElement }) {
  // The reading of props.children causes this error
  return props.children && props.children;

  // The workaround is this:
  //
  //   return "children" in props && props.children;
}

export default function App() {
  return (
    // This causes error shown in error.png
    <Foo>
      <div>Hello</div>
    </Foo>

    // These uses do not cause the error:
    //
    //   <Foo>Hello</Foo>
    //   <Foo><>Hello</></Foo>
  );
}

Expected behavior 🤔

I'd expect to be able to access an element from props to see if it's defined vs undefined/null without an error.

Steps to reproduce 🕹

In a terminal:

$ git clone https://github.com/gar1t/reproduce-solid-hydration-error.git
$ cd reproduce-solid-hydration-error
$ npm i
$ npm run dev

Open the link - the error should be displayed.

Context 🔦

The scenario is that I want to render a block that's conditional on an attribute being defined in props. This can be simply using something like <Show when={props.someElement}>...</Show>.

Your environment 🌎

@gar1t gar1t added the bug Something isn't working label Feb 1, 2025
@gar1t
Copy link
Author

gar1t commented Feb 2, 2025

I think this shows my lack of familiarity with the internals of how the props arg is constructed. It looks like its attributes are all getters and in the case of a component, the component function is called. So props.children && props.children (or any similar check of a property that returns a component) calls the component function twice.

So I think my report of this as a bug is spurious - though my surprise is real enough ;)

The use of "children" in props && props.children does.

@brenelz brenelz closed this as not planned Won't fix, can't repro, duplicate, stale Mar 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants