Last active
August 14, 2019 13:14
-
-
Save Tevinthuku/e5104095ffe35b270ac7994a87f59806 to your computer and use it in GitHub Desktop.
Clone fiber
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
// .. code | |
function cloneChildFibers(parentFiber) { | |
const oldFiber = parentFiber.alternate; | |
// if there is no child for the alternate | |
// there's no more work to do | |
// so just kill the execution | |
if (!oldFiber.child) { | |
return; | |
} | |
let oldChild = oldFiber.child; | |
// on initial render, the prevChild is null. | |
let prevChild = null; | |
/** | |
* below we are essencially looping through all the siblings | |
* so that can give them their new parent which is the workInProgress fiber | |
* the other properties are hard coded as well. | |
* I could have spread them but for understanding of the | |
* structure given, We are not going to spread them here. | |
*/ | |
while (oldChild) { | |
const newChild = { | |
type: oldChild.type, | |
tag: oldChild.tag, | |
stateNode: oldChild.stateNode, | |
props: oldChild.props, | |
partialState: oldChild.partialState, | |
alternate: oldChild, | |
parent: parentFiber | |
}; | |
if (prevChild) { | |
prevChild.sibling = newChild; | |
} else { | |
parentFiber.child = newChild; | |
} | |
prevChild = newChild; | |
oldChild = oldChild.sibling; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment