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

componentDidMount fired when DOM is not mounted for portal tree components #11597

Closed
frenzzy opened this issue Nov 20, 2017 · 1 comment
Closed

Comments

@frenzzy
Copy link

frenzzy commented Nov 20, 2017

Do you want to request a feature or report a bug?

BUG

What is the current behavior?

For all components inside react portal componentDidMount fired when DOM is not mounted yet.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar (template for React 16: https://jsfiddle.net/Luktwrdm/, template for React 15: https://jsfiddle.net/hmbg7e9w/).

Demo: https://jsfiddle.net/Luktwrdm/40/

What is the expected behavior?

componentDidMount being fired when DOM is mounted

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

Problem exist in React v16.1.1, browser or OS des not matter.
It worked fine earlier in React v15 with ReactDOM.unstable_renderSubtreeIntoContainer

@gaearon
Copy link
Collaborator

gaearon commented Nov 20, 2017

I don't think there is a bug in React.

In React, child's componentDidMount always fires before parents. This ensures the parent is ready and contains all children by the time it mounts (e.g. in case it needs to measure itself).

In your example App is inside of Portal. Therefore App.componentDidMount runs before Portal.componentDidMount. This is expected.

You are not seeing a number because by the time you do the measurement in App, your code in Portal has not yet added its node to the body yet. Therefore it is still in a detached tree.

To fix this, you could append this.node to the body immediately after creating it. Then you would be able to measure its child immediately when the child mounts.

@gaearon gaearon closed this as completed Nov 20, 2017
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