Last active
July 8, 2018 14:49
-
-
Save ManasJayanth/43f93bffa5499888a88684100543518f to your computer and use it in GitHub Desktop.
Snippets for Learn you some custom react renderers
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
const hostConfig = { | |
getRootHostContext(rootContainerInstance) { | |
}, | |
getChildHostContext(parentHostContext, type, rootContainerInstance) { | |
}, | |
getPublicInstance(instance) { | |
}, | |
prepareForCommit(containerInfo) { | |
}, | |
resetAfterCommit(containerInfo) { | |
}, | |
createInstance( | |
type, | |
props, | |
rootContainerInstance, | |
hostContext, | |
internalInstanceHandle | |
) { | |
}, | |
appendInitialChild(parentInstance, child) { | |
}, | |
finalizeInitialChildren( | |
domElement, | |
type, | |
props, | |
rootContainerInstance, | |
hostContext | |
) { | |
}, | |
prepareUpdate( | |
domElement, | |
type, | |
oldProps, | |
newProps, | |
rootContainerInstance, | |
hostContext | |
) { | |
}, | |
shouldSetTextContent(type, props) { | |
}, | |
shouldDeprioritizeSubtree(type, props) { | |
}, | |
createTextInstance( | |
text, | |
rootContainerInstance, | |
hostContext, | |
internalInstanceHandle | |
) { | |
}, | |
now: null, | |
isPrimaryRenderer: true, | |
scheduleDeferredCallback: "", | |
cancelDeferredCallback: "", | |
// ------------------- | |
// Mutation | |
// ------------------- | |
supportsMutation: true, | |
commitMount(domElement, type, newProps, internalInstanceHandle) { | |
}, | |
commitUpdate( | |
domElement, | |
updatePayload, | |
type, | |
oldProps, | |
newProps, | |
internalInstanceHandle | |
) { | |
}, | |
resetTextContent(domElement) { | |
}, | |
commitTextUpdate(textInstance, oldText, newText) { | |
}, | |
appendChild(parentInstance, child) { | |
}, | |
appendChildToContainer(container, child) { | |
}, | |
insertBefore(parentInstance, child, beforeChild) { | |
}, | |
insertInContainerBefore(container, child, beforeChild) { | |
}, | |
removeChild(parentInstance, child) { | |
}, | |
removeChildFromContainer(container, child) { | |
} | |
}; |
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
import ReactReconciler from 'react-reconciler'; | |
const hostConfig = { | |
getRootHostContext(rootContainerInstance) { | |
return {} | |
}, | |
getChildHostContext(parentHostContext, type, rootContainerInstance) { | |
return {}; | |
}, | |
getPublicInstance(instance) { | |
console.log('getPublicInstance'); | |
}, | |
prepareForCommit(containerInfo) { | |
}, | |
resetAfterCommit(containerInfo) { | |
}, | |
createInstance( | |
type, | |
props, | |
rootContainerInstance, | |
hostContext, | |
internalInstanceHandle | |
) { | |
return document.createElement(type); | |
}, | |
appendInitialChild(parentInstance, child) { | |
parentInstance.appendChild(child) | |
}, | |
finalizeInitialChildren( | |
domElement, | |
type, | |
props, | |
rootContainerInstance, | |
hostContext | |
) { | |
const { children, ...otherProps } = props; | |
Object.keys(otherProps).forEach(attr => { | |
if (attr === 'className') { | |
domElement.class = otherProps[attr]; | |
} else if (attr === 'onClick') { | |
const listener = otherProps[attr]; | |
if (domElement.__ourVeryHackCacheOfEventListeners) { | |
domElement.__ourVeryHackCacheOfEventListeners.push(listener) | |
} else { | |
domElement.__ourVeryHackCacheOfEventListeners = [ listener ] | |
} | |
domElement.addEventListener('click', listener); | |
} else { | |
throw new Error('TODO: We haven\'t handled other properties/attributes') | |
} | |
}) | |
}, | |
prepareUpdate( | |
domElement, | |
type, | |
oldProps, | |
newProps, | |
rootContainerInstance, | |
hostContext | |
) { | |
console.log('prepareUpdate'); | |
return [ null ]; | |
}, | |
shouldSetTextContent(type, props) { | |
return false; | |
}, | |
shouldDeprioritizeSubtree(type, props) { | |
console.log('shouldDeprioritizeSubtree'); | |
}, | |
createTextInstance( | |
text, | |
rootContainerInstance, | |
hostContext, | |
internalInstanceHandle | |
) { | |
return document.createTextNode(text); | |
}, | |
now: Date.now, | |
isPrimaryRenderer: true, | |
scheduleDeferredCallback: "", | |
cancelDeferredCallback: "", | |
// ------------------- | |
// Mutation | |
// ------------------- | |
supportsMutation: true, | |
commitMount(domElement, type, newProps, internalInstanceHandle) { | |
console.log('commitMount'); | |
}, | |
commitUpdate( | |
domElement, | |
updatePayload, | |
type, | |
oldProps, | |
newProps, | |
internalInstanceHandle | |
) { | |
}, | |
resetTextContent(domElement) { | |
}, | |
commitTextUpdate(textInstance, oldText, newText) { | |
textInstance.nodeValue = newText; | |
}, | |
appendChild(parentInstance, child) { | |
}, | |
appendChildToContainer(container, child) { | |
container.appendChild(child) | |
}, | |
insertBefore(parentInstance, child, beforeChild) { | |
console.log('insertBefore'); | |
}, | |
insertInContainerBefore(container, child, beforeChild) { | |
console.log('insertInContainerBefore'); | |
}, | |
removeChild(parentInstance, child) { | |
console.log('removeChild'); | |
}, | |
removeChildFromContainer(container, child) { | |
console.log('removeChildFromContainer'); | |
} | |
}; | |
const DOMRenderer = ReactReconciler(hostConfig); | |
let internalContainerStructure; | |
export default { | |
render(elements, containerNode, callback) { | |
// We must do this only once | |
if (!internalContainerStructure) { | |
internalContainerStructure = DOMRenderer.createContainer( | |
containerNode, | |
false, | |
false | |
); | |
} | |
DOMRenderer.updateContainer(elements, internalContainerStructure, null, callback); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment