fix: fix recordHostChildrenToDelete for fragment deletion#43
Open
Russellwzr wants to merge 19 commits intoBetaSu:masterfrom
Open
fix: fix recordHostChildrenToDelete for fragment deletion#43Russellwzr wants to merge 19 commits intoBetaSu:masterfrom
Russellwzr wants to merge 19 commits intoBetaSu:masterfrom
Conversation
fix: dispatch
fix: 修复updateHostRoot中的类型问题
BetaSu
reviewed
Jul 6, 2023
| } | ||
| node = node.sibling; | ||
| function recordHostChildrenToDelete(beginNode: FiberNode): FiberNode[] { | ||
| if (beginNode.tag !== Fragment) return [beginNode]; |
Owner
There was a problem hiding this comment.
recordHostChildrenToDelete方法应该是找到第一层Host类型节点,这里的实现看起来是找到第一层“非Fragment”类型节点?
Author
There was a problem hiding this comment.
感谢提醒,当时确实只把关注点落在了修复多层级的Fragment上而忽略了其他情况,额外加上Function Component的判断应该就可以了,本地又测试了下~
BetaSu
reviewed
Jul 6, 2023
| } | ||
| node = node.sibling; | ||
| function recordHostChildrenToDelete(beginNode: FiberNode): FiberNode[] { | ||
| if (beginNode.tag !== Fragment && beginNode.tag !== FunctionComponent) |
Owner
There was a problem hiding this comment.
相比于 判断 !== ,可以抽一个方法判断当前节点是 Host类型,类似:
function isHostTypeFiberNode(fiber: FiberNode) {
const tag = fiber.tag;
return [HostComponent, HostRoot, HostText].includes(tag);
}
BetaSu
reviewed
Jul 6, 2023
| const hostChildrenToDelete: FiberNode[] = | ||
| recordHostChildrenToDelete(childToDelete); | ||
|
|
||
| for (let i = 0; i < hostChildrenToDelete.length; i++) { |
Owner
There was a problem hiding this comment.
这里的实现似乎有些问题,不应该commitNestedUnmounts(hostChildrenToDelete[i],这样会丢失一些非Host类型组件的遍历(比如FC)
Author
There was a problem hiding this comment.
对的,还是一开始关注点偏离了FC的问题,这里需要恢复传参为childToDelete.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
原代码中标记Fragment下需要被删除的元素时,没有考虑到Fragment嵌套的情况,会导致部分待删除元素被忽略,可用原代码运行如下组件进行复现:
改进策略为:修改recordHostChildrenToDelete,通过BFS标记各层Fragment下需要删除的元素,并将recordHostChildrenToDelete与commitNestedUnmounts解耦,增强代码可读性。