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

Double mounting for children element. #2147

Closed
d8corp opened this issue Apr 27, 2024 · 2 comments
Closed

Double mounting for children element. #2147

d8corp opened this issue Apr 27, 2024 · 2 comments

Comments

@d8corp
Copy link

d8corp commented Apr 27, 2024

Describe the bug

Check the next code:

import { render } from "solid-js/web";
import { onMount } from "solid-js";

function Button(props: any) {
  return (
    <button class={props.children ? "withChildren" : undefined}>
      {props.children}
    </button>
  );
}

function Test() {
  onMount(() => {
    console.log("???");
  });

  return <span>Test</span>;
}

render(
  () => (
    <Button>
      <Test />
    </Button>
  ),
  document.getElementById("app")!,
);

Expected result: console.log("???") runs once
Actual result: console.log("???") runs twice

Your Example Website or App

https://playground.solidjs.com/

Steps to Reproduce the Bug or Issue

  1. Copy the code in description
  2. Pass the code in playground
  3. Check console output

Expected behavior

Expected result: console.log("???") runs once

Screenshots or Videos

No response

Platform

  • OS: [e.g. macOS, Windows, Linux]
  • Browser: [e.g. Chrome, Safari, Firefox]
  • Version: [e.g. 91.1]

Additional context

No response

@d8corp
Copy link
Author

d8corp commented Apr 27, 2024

Fixed by children function from solid-js

import { render } from "solid-js/web";
import { onMount, children } from "solid-js";

function Button(props: any) {
  const memo_children = children(() => props.children);

  return (
    <button class={memo_children() ? "withChildren" : undefined}>
      {memo_children()}
    </button>
  );
}

function Test() {
  onMount(() => {
    console.log("???");
  });

  return <span>Test</span>;
}

render(
  () => (
    <Button>
      <Test />
    </Button>
  ),
  document.getElementById("app")!,
);

@ryansolid
Copy link
Member

Yeah this is by design. Children are executed lazily and created on access. So if you need to access children multiple times use the helper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants