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

Add default mounted() method to Component trait #583

Merged
merged 3 commits into from
Aug 13, 2019

Conversation

hgzimmerman
Copy link
Member

@hgzimmerman hgzimmerman commented Aug 12, 2019

Fixes #572

I made the choice to have on_mount() not return a ShouldRender bool, because I don't think there is a situation where altering parts of a component state relevant to the view() should ever take place in on_mount() instead of create(). Let me know your thoughts, because its an easy enough change to make.

@hgzimmerman
Copy link
Member Author

Actually, the thought just occurred to me that on_mount() could be used to grab a reference to an element introduced in the initial call to view(). One application of this could be to control Canvas elements, which you would likely want to re-render as soon as you initialize the canvas using stdweb.

Let me know if you agree that on_mount() should optionally cause a re-draw.

@jstarry
Copy link
Member

jstarry commented Aug 13, 2019

@hgzimmerman yeah let's follow the lead of React and allow re-rendering on mount: https://reactjs.org/docs/react-component.html#componentdidmount

Their stated use-case of dialogs getting the width of the screen for example is something we should support. Also, like React, we will not update the browser screen until the mount is finished.

src/html/mod.rs Outdated Show resolved Hide resolved
src/html/mod.rs Outdated Show resolved Hide resolved
src/html/scope.rs Outdated Show resolved Hide resolved
@hgzimmerman
Copy link
Member Author

I think this accomplishes what you want.

The only point of ambiguity that I have is about your comment here:

Also, like React, we will not update the browser screen until the mount is finished.

Does calling update() exclusively handle vdom stuff and the steps for rendering to the screen are handled by another part of Yew's internals? Because update() can be called twice with what I have here, and I would like to avoid drawing to the screen twice if that is part of the functionality present within update().

@hgzimmerman hgzimmerman changed the title Add default on_mount() method to Component trait Add default mounted() method to Component trait Aug 13, 2019
@jstarry
Copy link
Member

jstarry commented Aug 13, 2019

Yup, this is exactly what I had in mind. My understanding is that since the environment is single threaded, the browser won't update the screen until your code finishes.

Copy link
Member

@jstarry jstarry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks!!

@jstarry
Copy link
Member

jstarry commented Aug 13, 2019

bors r+

bors bot added a commit that referenced this pull request Aug 13, 2019
583: Add default mounted() method to Component trait r=jstarry a=hgzimmerman

Fixes #572

I made the choice to have `on_mount()` not return a `ShouldRender` bool, because I don't think there is a situation where altering parts of a component state relevant to the `view()` should ever take place in `on_mount()` instead of `create()`. Let me know your thoughts, because its an easy enough change to make.

Co-authored-by: Henry Zimmerman <zimhen7@gmail.com>
@bors
Copy link
Contributor

bors bot commented Aug 13, 2019

Build succeeded

  • continuous-integration/travis-ci/push

@bors bors bot merged commit 1ef1b1a into yewstack:master Aug 13, 2019
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

Successfully merging this pull request may close these issues.

Feature: on_mount() component lifecycle method
2 participants