Skip to content

Instantly share code, notes, and snippets.

@Tevinthuku
Last active August 13, 2019 21:19
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/53ae83f42f351ef50067de6f696ce183 to your computer and use it in GitHub Desktop.
Save Tevinthuku/53ae83f42f351ef50067de6f696ce183 to your computer and use it in GitHub Desktop.
updated reconciler function.
export function reconcile(parentDom, instance, element) {
if (instance == null) {
// initial render
const newInstance = instantiate(element);
parentDom.appendChild(newInstance.dom);
return newInstance;
} else if (element == null) {
/**
* this section gets hit when
* a childElement was previously present
* but in the new element is not present
* for instance a todo item that has been deleted
* it was present at first but is now not present
*/
parentDom.removeChild(instance.dom);
return null;
} else if (instance.element.type !== element.type) {
/**
* if the type of the previous Instance is not the
* same as the type of the new element
* we replace the old with the new.
* eg: if we had an "input" and now have "button"
* we get rid of the input and replace it with the button
*/
const newInstance = instantiate(element);
parentDom.replaceChild(newInstance.dom, instance.dom);
return newInstance;
} else if (typeof element.type === "string") {
/**
* if the types are the same & are HTMLElement types
* eg: if prevType was "input" and current type is still "input"
* NB:// we still havent updated
* the props of the node rendered in the dom
*/
instance.childInstances = reconcileChildren(instance, element);
instance.element = element;
return instance;
} else {
//Update instance
instance.publicInstance.props = element.props;
const childElement = instance.publicInstance.render();
const oldChildInstance = instance.childInstance;
const childInstance = reconcile(parentDom, oldChildInstance, childElement);
instance.dom = childInstance.dom;
instance.childInstance = childInstance;
instance.element = element;
return instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment