Skip to content

Instantly share code, notes, and snippets.

@Tevinthuku
Last active August 14, 2019 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tevinthuku/e5104095ffe35b270ac7994a87f59806 to your computer and use it in GitHub Desktop.
Save Tevinthuku/e5104095ffe35b270ac7994a87f59806 to your computer and use it in GitHub Desktop.
Clone fiber
// .. 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