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

refs are not resolved #55

Open
manast opened this issue Feb 13, 2024 · 3 comments
Open

refs are not resolved #55

manast opened this issue Feb 13, 2024 · 3 comments

Comments

@manast
Copy link

manast commented Feb 13, 2024

I apologize if this issue is known, or if it does not belong to this repo, but I could not find any answer anywhere, including ChatGPT.

Consider this dummy component:

import { Component, onMount } from "solid-js";

const Button: Component<void> = () => {
  let myButton: HTMLButtonElement;

  onMount(() => {
    console.log({ myButton });
  });

  return <button ref={myButton!}>Text</button>;
};

export default Button;

When running this component, the button element will be printed on the console, however when running the following test the ref is undefined

import { describe, it } from "vitest";
import { render } from "@solidjs/testing-library";
import Button from "./button";

describe("Button Component", () => {
  it("renders correctly", async () => {
    render(() => <Button />);
  });
});

Any ideas?

@atk
Copy link
Collaborator

atk commented Feb 13, 2024

Effects run asynchronously, so this is expected behavior and not an error. In order to check the ref, you need to await until the button can be found in the DOM.

@manast
Copy link
Author

manast commented Feb 14, 2024

Effects run asynchronously, so this is expected behavior and not an error. In order to check the ref, you need to await until the button can be found in the DOM.

But thats exactly why onMount is used, to wait for all the components to be mounted in the DOM. Note that the code above is the recommended way to set refs in SolidJS... (and works in the browser perfectly as it should).

@atk
Copy link
Collaborator

atk commented Feb 14, 2024

I just tested it. I can see the console output: { myButton: HTMLButtonElement {} }. I literally just added your component and test to the ts-vitest template's tests.

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