Skip to content

how do i get the height of an element post-render? #342

Answered by gbj
ghost asked this question in Q&A
Discussion options

You must be logged in to vote

Yeah the NodeRef is filled on element creation, not on mounting to the DOM.

It might be a little hacky but how about something like this, using request_animation_frame to delay until it's mounted. (I haven't tried this to see if it actually works.)

let elem_ref = NodeRef::new(cx);

create_effect(cx, move |_| {
    if let Some(elem) = elem_ref.get() {
       request_animation_frame(move || {
           console_log(&format!("{:?}", elem.get_bounding_client_rect().height()));
       });
    }
});

view! {cx,
    <div _ref=elem_ref>
        <div height=10 width=3 />
    </div>
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ghost
Comment options

Answer selected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant