Description
Summary:
ref is called after componentDidUpdate
Steps to reproduce:
- Attach a ref in any component in the children of the modal
- The componentDidUpdate will get called before ref get attached to any children of the modal.
Expected behavior:
ref should get attached before componentDidUpdate
Additional notes:
I created a couple codepens to further explain this issue.
This codepen will reproduce this bug. Simply open the console, and click the Trigger Modal button. You should see two log messages, "componentDidUpdate" and follow by a "attach ref".
This codepen will show a normal react lift cycle if I remove change the Modal component to a div. When you click Trigger Modal button, you should see two "attach ref" follow by a "componentDidUpdate" after clicking the Tigger Modal button.
This codepen is a workaround I discovered. Open up the console, and click Tigger Modal, then click the number button. You should see two "attach ref" follow by a "componentDidUpdate" after clicking the number button.
Basically, if you can handle all the state and update in a component without declaring as the direct children of Modal, then you can create another component to wrap around all the logic, and put that component as the child of the Modal component.
I think this behavior would surprise a lot of people if they need the correct sequence of attaching ref and componentDidUpdate. I skimmed through the source a little bit, and I think it's likely relate to how ModalPortal get created. I think at the very least this needs to be either documented or fixed.