Skip to content

How to get reference to rendered element after mounting? #36

Description

@bluenote10

I was searching the docs for the best practices for getting a ref to an element rendered by Solid. Use case: Passing an element reference to an external library.

I could probably just give it an id, create an effect, and use getElementById, but that looks like an anti-pattern in Solid.

The afterRender directive sounds like it does the job, but from the docs I cannot infer how it works, and it only exists for when and each. Is there something similar for an unconditional <div>?

Regarding getting the element reference I could simple use:

// example use of a plotting library
function Example() {
  let el = <div></div>
  // pass el to external library
  return el;
}

but than I'm missing lifecycle handling, i.e., the external library may require to receive el only after it gets mounted. The use case is plotting libraries that can only plot into the given element after the element has become visible, because they perform internal size measurements.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions