Last active
August 14, 2019 09:58
-
-
Save Tevinthuku/512b3758bcab1475d155add4d5fe5631 to your computer and use it in GitHub Desktop.
This file contains 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 beginWork(wipFiber) { | |
if (wipFiber.tag == CLASS_COMPONENT) { | |
updateClassFiber(wipFiber); | |
} else { | |
updateHostFiber(wipFiber); | |
} | |
} | |
function updateHostFiber(wipFiber) { | |
if (!wipFiber.stateNode) { | |
// if this is the initialRender and stateNode is null | |
// create a new node. | |
wipFiber.stateNode = createDomElement(wipFiber); | |
} | |
const newChildElements = wipFiber.props.children; | |
reconcileChildrenArray(wipFiber, newChildElements); | |
} | |
function updateClassFiber(wipFiber) { | |
let instance = wipFiber.stateNode; | |
if (instance == null) { | |
// if this is the initialRender call the constructor | |
instance = wipFiber.stateNode = createInstance(wipFiber); | |
} else if (wipFiber.props == instance.props && !wipFiber.partialState) { | |
// nothing has changed here | |
// lets move to the children | |
cloneChildFibers(wipFiber); | |
return; | |
} | |
instance.props = wipFiber.props; | |
instance.state = Object.assign({}, instance.state, wipFiber.partialState); | |
wipFiber.partialState = null; | |
const newChildElements = wipFiber.stateNode.render(); | |
reconcileChildrenArray(wipFiber, newChildElements); | |
} | |
function createInstance(fiber) { | |
//similar to the previous implementation | |
// we instanciate a new object of the class provided in the | |
// type prop and return the new instance | |
const instance = new fiber.type(fiber.props); | |
instance.__fiber = fiber; | |
return instance; | |
} | |
function createDomElement(fiber) { | |
// check the type of the fiber object. | |
const isTextElement = fiber.type === TEXT_ELEMENT; | |
const dom = isTextElement | |
? document.createTextNode("") | |
: document.createElement(fiber.type); | |
updateDomProperties(dom, [], fiber.props); | |
return dom; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment