Repository navigation
Renderers can queue commit effects for initial mount - #8646
Conversation
c443eed to
4d1d0c4
Compare
| // Life-cycles happen as a separate pass so that all placements, updates, | ||
| // and deletions in the entire tree have already been invoked. | ||
| // This pass also manages focus since components must be in the DOM first, | ||
| // And this does not happen untilt he entire tree is mounted. |
|
I am not 100% this works but shouldn't you be able to use prepareForCommit/resetAfterCommit to do this? |
4d1d0c4 to
0c1595d
Compare
|
IMO we should not have the reconciler managing focusing state. It should be up to the renderer to do that if we can't do that with currently exposed methods from the reconciler we should introduce new API methods such as shouldFocusHostComponent(type, props) and focusHostComponent(instance). |
|
Hey @edvinerikson 😄 Yeah, I believe this could also be done in |
| case 'button': | ||
| case 'input': | ||
| case 'select': | ||
| case 'textarea': |
There was a problem hiding this comment.
These types are specific to DOM renderer so they shouldn’t be in the reconciler.
| case 'input': | ||
| case 'select': | ||
| case 'textarea': | ||
| if (newProps.autoFocus) { |
There was a problem hiding this comment.
As is this particular prop name.
There was a problem hiding this comment.
Right 👍 Sorry. This wasn't quite ready for review yet. I probably shouldn't have opened a PR yet. I just like to track my thoughts-in-progress via PR. Once it's ready I'll assign a reviewer.
There was a problem hiding this comment.
Yeah no worries. A convention I've been using is to tag PR with [WIP] and maybe mention in the description what isn't ready yet.
There was a problem hiding this comment.
Good call! I've updated the description to be more clear. 😁
I thought everyone else was on holiday today!
There was a problem hiding this comment.
I sort of am but having a lazy evening. 😛
|
|
||
| // Auto-focus host components once they have been mounted. | ||
| if (nextEffect.effectTag & Focus) { | ||
| focusNode(nextEffect.stateNode); |
There was a problem hiding this comment.
Similarly this is a DOM-specific utility function and shouldn't get called in non-DOM environments.
|
Very helpful initial feedback by @edvinerikson. (Thanks!) After looking at So I've updated this PR to add 2 new
|
265f7e7 to
b5ed656
Compare
|
I believe this PR is ready for review now. 😁 I don't want to assign it to @gaearon since you're on holiday, but if you are bored and want to take a look I'd value your feedback. 😉 Else maybe @acdlite can pick it up tomorrow? I'm out for the evening to catch a movie with my parents. Will check back in a few hours! |
|
Another thought: What if we store these DOM nodes in the rootHostContainer when they are created / updated and use that in resetAfterCommit? Should be possible if we send the rootHostContainer to resetAfterCommit. Something like: rootHostContainer._focusedElement.focus() in resetAfterCommit. Or maybe this will cause memory leaks if we don't remove instances if work gets aborted. |
|
Don't have time to look at this closely until tomorrow, but it'd be best to solve this without a new effect type. @sebmarkbage's idea was to reuse the |
|
This can hold until tomorrow @acdlite. I'm done for the night in Virginia anyway. 😄 Maybe we can chat when you get in tomorrow. I don't initially see how we can re-use the I don't know what Sebastian may have had in mind about this. All I had to go off of was the TODO comment in |
|
@edvinerikson Yes, that's what I was worried about- cleaning up after cases where we get interrupted before completion, etc. Seemed safer this way. |
|
Even "focus" is a pretty specific concept that likely does not apply to all renderers. Having some way for components in the host tree to opt into a commit-time effect for initial mount makes sense to me. |
|
@bvaughn As Ben said, what we need is a "commit-time effect for initial mount" — like I don't think adding another method to the host config is necessary. We already have |
acdlite
left a comment
There was a problem hiding this comment.
- Remove
Focusside-effect. TheUpdateeffect should be sufficient. - Instead of a new host config method, have
finalizeInitialChildrenreturn a boolean to indicate if a host component needs anUpdateeffect on initial mount.
Great point. Thanks for the suggestions Andrew and Ben. I'll implement them sometime this morning and get back. 😄 |
Couple of snags with this:
My thoughts are that we should add a new renderer method (eg We also need to wait to call this method until mounting has finished. I don't feel strongly about the best way to handle this, although I lean toward using I'd love to know your thoughts on the above. 😄 Update: I expect some discussion and changes will be required but I'm going to go ahead and push what I suggested above for discussion purposes (see 09229ef). |
d2dd297 to
1d23b4e
Compare
| // aka when there is no current/alternate. | ||
| if ( | ||
| !current && | ||
| nextEffect.effectTag & Update |
There was a problem hiding this comment.
Need an additional check that the tag is HostComponent.
| } | ||
|
|
||
| function commitAllLifeCycles() { | ||
| function commitAllLifeCyclesAndApplyInitialEffects() { |
There was a problem hiding this comment.
Let's keep the previous name. What you're calling an "initial effect" could also be used to describe componentDidMount. I think calling this a "lifecycle" is sufficient.
| const props = nextEffect.memoizedProps; | ||
| const instance : I = nextEffect.stateNode; | ||
| const rootContainerInstance = getRootHostContainer(); | ||
| commitInitialEffects(instance, type, props, rootContainerInstance, nextEffect); |
There was a problem hiding this comment.
How about commitInitialMount?
|
Also since the original commits have essentially been reverted, would you mind either rebasing or opening up a new PR? |
There's a lot of useful discussion on this PR (so opening a new PR seems overkill). I'm leaving the commits in place to make it easier for anyone interested in doing an incremental review. I rebase and squash things at the end of a review, before merging. This is part of my normal workflow. |
|
Yeah I just mean before it's merged. |
Oh, gotcha. Yup! Squash before merge, for sure. |
| // aka when there is no current/alternate. | ||
| if ( | ||
| !current && | ||
| nextEffect.tag === HostComponent && |
There was a problem hiding this comment.
Hmm how about we move this to commitLifeCycles? Avoids an extra check. https://github.com/facebook/react/blob/master/src/renderers/shared/fiber/ReactFiberCommitWork.js#L434
5c95952 to
5f4e681
Compare
| return false; | ||
| }, | ||
|
|
||
| focusHostComponent(instance : Instance) : void { |
|
Delete that one leftover method and this is good to merge. We can continue bikeshedding on the name and come back to it later. |
The finalizeInitialChildren HostConfig method now utilizes a boolean return type. Renderers can return true to indicate that custom effects should be processed at commit-time once host components have been mounted. This type of work is marked using the existing Update flag. A new HostConfig method, commitMount, has been added as well for performing this type of work. This change set is in support of the autoFocus prop.
5f4e681 to
a101313
Compare
| // aka when there is no current/alternate. | ||
| if ( | ||
| !current && | ||
| finishedWork.effectTag & Update |
There was a problem hiding this comment.
Oh shoot, missed this. Don't need to check for Update effect because it was already checked by the time commitLifeCycles is called.
There was a problem hiding this comment.
If that's the case, why does the ClassComponent case check for it: https://github.com/facebook/react/blob/9e461c715cb22523764b37c783fd37cf9b878fb5/src/renderers/shared/fiber/ReactFiberCommitWork.js#L406
There was a problem hiding this comment.
Because it also gets called if there's a Callback effect, which host components don't have. I guess it's a bit weird either way, so I'm fine with keeping this as-is.
There was a problem hiding this comment.
Ah, right. That makes sense. Ok!
|
Huge thanks to @acdlite and others for the above review. Learned a lot! |
| createInstance(type : T, props : P, rootContainerInstance : C, hostContext : CX, internalInstanceHandle : OpaqueNode) : I, | ||
| appendInitialChild(parentInstance : I, child : I | TI) : void, | ||
| finalizeInitialChildren(parentInstance : I, type : T, props : P, rootContainerInstance : C) : void, | ||
| finalizeInitialChildren(parentInstance : I, type : T, props : P, rootContainerInstance : C) : boolean, |
There was a problem hiding this comment.
We could consider returning an constant enum number tag here. Like other tags. It's not less efficient as long as it gets inlined and the constants make it a bit more readable. It's also more extensible if we want to add more tags here.
There was a problem hiding this comment.
True! What other sorts of work do you think finalizeInitialChildren might want to schedule? (I'd prefer to think of at least one other likely use case before adding a constant.)
| case 'input': | ||
| case 'select': | ||
| case 'textarea': | ||
| return !!(props : any).autoFocus; |
There was a problem hiding this comment.
We should expand the Props type to include this as optional instead of relying on any here.
| internalInstanceHandle : Object, | ||
| ) : void { | ||
| if (shouldAutoFocusHostComponent(type, newProps)) { | ||
| (domElement : any).focus(); |
There was a problem hiding this comment.
You can cast this to something more specific that has the focus method on it.
Such as ((domElement : any) : HTMLInputElement). That way we know what it should be if we properly typed this.
There was a problem hiding this comment.
You are no longer using the focusNode helper which means that this will start throwing in IE8 for hidden elements which is a breaking change. Do we really want that?
There was a problem hiding this comment.
Aye, the PR summary noted that:
IE8 compatibility note
Note that this PR also drops use of the
focusNodehelper method since it was in place for IE8 and IE8 support was officially dropped in January 2016.
Seemed wrong to keep an abstraction around solely for compat with a browser version we no longer support.
| rootContainerInstance : Container, | ||
| internalInstanceHandle : Object, | ||
| ) : void { | ||
| if (shouldAutoFocusHostComponent(type, newProps)) { |
There was a problem hiding this comment.
Is this extra shouldAutoFocusHostComponent really necessary? Won't this only be scheduled if the first one is true? At least after #8685.
There was a problem hiding this comment.
This check is still necessary. commitMount will be called for host components with refs even if finalizeInitialChildren returns false. (The "preserves focus" test in ReactDOM-test covers this case indirectly.)
There was a problem hiding this comment.
But #8685 added a special tag for refs so it shouldn't be necessary for it to call commitMount in that case anymore, right?
There was a problem hiding this comment.
Hm, I think you're right, but there's this: https://github.com/facebook/react/blob/3bc5595dfd6db741b19f17e7aef98b2c1e4be847/src/renderers/shared/fiber/ReactFiberCompleteWork.js#L240-L243
I think this is actually not intentional. I'll follow up on it. 😄
There was a problem hiding this comment.
Yeah that was an oversight on my part
Renderers can queue commit effects for initial mount
Also removed an unnecessary conditional check and improved a flow cast. Relates originally to PRs react#8646 and react#8685
Renderers can queue commit effects for initial mount
Also removed an unnecessary conditional check and improved a flow cast. Relates originally to PRs react#8646 and react#8685
Auto-focus was previously managed by
ReactDOMFiberComponent.setInitialProperties(which gets called during the complete phase). This did not work because host components are not yet present in the DOM until the entire tree has been mounted (which happens during the commit phase).This PR resolves that by making a few changes:
HostConfigfinalizeInitialChildrenmethod now returns a boolean. Renderers should return true to indicate that custom effects should be processed during commit (once host components have been mounted). egReactDOMFiberuses this to queue auto-focus.HostConfigmethod,commitMount, has been added for performing this type of work. (See this discussion thread for why a new method was added instead of repurposingcommitUpdate.) Existing renderers have been updated with noops.Tests
I was able to easily reproduce the broken focus behavior in a browser and verify the fix. However it was a bit awkward to catch in a unit test because PhantomJS does not require DOM elements to be mounted in order to track their focused state. I was able to add a test that caught the broken behavior and verifies the fix, but it's kind of a hack.
IE8 compatibility note
Note that this PR also drops use of the
focusNodehelper method since it was in place for IE8 and IE8 support was officially dropped in January 2016.