Skip to content

Instantly share code, notes, and snippets.



Last active Aug 14, 2019
What would you like to do?
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) {
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