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

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

Closed
esindger opened this issue Nov 10, 2019 · 2 comments
Closed

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

esindger opened this issue Nov 10, 2019 · 2 comments

Comments

@esindger
Copy link

Hello,
I did not found any life cycle hooks. What is the recommended way to get reference to rendered element?

@ryansolid
Copy link
Owner

Yes you are correct. There are no render based lifecycles. There is only creation and cleanup. And then a bunch of discreet computation executions. Every JSX element is an actual DOM node that is rendered. So you can use ref or forwardRef. ref assigns to a variable and forwardRef uses a function form.

let elRef;
setTimeout(() => elRef.clientWidth);
<div ref={elRef} />

or

let elRef;
setTimeout(() => elRef.clientWidth);
<div forwardRef={ref => elRef = ref} />

Technically speaking refs resolve at time of node creation before being connected to the live DOM so I usually end up setting some sort of delay or wrap it some sort of computed to access it. If you want to simulate the timing of like React's componentDidMount, I'd go for Promise.resolve().then(() => /*... */);. And for componentWillUnmount use mobx-jsx's cleanup method. The rest of the life cycles don't really apply as it will be managed through mobx's reactive system.

@esindger
Copy link
Author

Thank you for the awesome explanation!

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