Created
June 20, 2018 20:24
-
-
Save agenthunt/e4f34743f55134cef75bc5ae335f3788 to your computer and use it in GitHub Desktop.
hello-react-custom-renderer-myCustomRenderer.js
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'; | |
| function traceWrap(hostConfig) { | |
| let traceWrappedHostConfig = {}; | |
| Object.keys(hostConfig).map(key => { | |
| const func = hostConfig[key]; | |
| traceWrappedHostConfig[key] = (...args) => { | |
| console.trace(key); | |
| return func(...args); | |
| }; | |
| }); | |
| return traceWrappedHostConfig; | |
| } | |
| const rootHostContext = {}; | |
| const childHostContext = {}; | |
| const hostConfig = { | |
| now: Date.now, | |
| getRootHostContext: () => { | |
| return rootHostContext; | |
| }, | |
| prepareForCommit: () => {}, | |
| resetAfterCommit: () => {}, | |
| getChildHostContext: () => { | |
| return childHostContext; | |
| }, | |
| shouldSetTextContent: (type, props) => { | |
| return typeof props.children === 'string' || typeof props.children === 'number'; | |
| }, | |
| /** | |
| This is where react-reconciler wants to create an instance of UI element in terms of the target. Since our target here is the DOM, we will create document.createElement and type is the argument that contains the type string like div or img or h1 etc. The initial values of domElement attributes can be set in this function from the newProps argument | |
| */ | |
| createInstance: (type, newProps, rootContainerInstance, _currentHostContext, workInProgress) => { | |
| const domElement = document.createElement(type); | |
| Object.keys(newProps).forEach(propName => { | |
| const propValue = newProps[propName]; | |
| if (propName === 'children') { | |
| if (typeof propValue === 'string' || typeof propValue === 'number') { | |
| domElement.textContent = propValue; | |
| } | |
| } else if (propName === 'onClick') { | |
| domElement.addEventListener('click', propValue); | |
| } else if (propName === 'className') { | |
| domElement.setAttribute('class', propValue); | |
| } else { | |
| const propValue = newProps[propName]; | |
| domElement.setAttribute(propName, propValue); | |
| } | |
| }); | |
| return domElement; | |
| }, | |
| createTextInstance: text => { | |
| return document.createTextNode(text); | |
| }, | |
| appendInitialChild: (parent, child) => { | |
| parent.appendChild(child); | |
| }, | |
| appendChild(parent, child) { | |
| parent.appendChild(child); | |
| }, | |
| finalizeInitialChildren: (domElement, type, props) => {}, | |
| supportsMutation: true, | |
| appendChildToContainer: (parent, child) => { | |
| parent.appendChild(child); | |
| }, | |
| prepareUpdate(domElement, oldProps, newProps) { | |
| return true; | |
| }, | |
| commitUpdate(domElement, updatePayload, type, oldProps, newProps) { | |
| Object.keys(newProps).forEach(propName => { | |
| const propValue = newProps[propName]; | |
| if (propName === 'children') { | |
| if (typeof propValue === 'string' || typeof propValue === 'number') { | |
| domElement.textContent = propValue; | |
| } | |
| } else { | |
| const propValue = newProps[propName]; | |
| domElement.setAttribute(propName, propValue); | |
| } | |
| }); | |
| }, | |
| commitTextUpdate(textInstance, oldText, newText) { | |
| textInstance.text = newText; | |
| }, | |
| removeChild(parentInstance, child) { | |
| parentInstance.removeChild(child); | |
| } | |
| }; | |
| const ReactReconcilerInst = ReactReconciler(traceWrap(hostConfig)); | |
| export default { | |
| render: (reactElement, domElement, callback) => { | |
| // Create a root Container if it doesnt exist | |
| if (!domElement._rootContainer) { | |
| domElement._rootContainer = ReactReconcilerInst.createContainer(domElement, false); | |
| } | |
| // update the root Container | |
| return ReactReconcilerInst.updateContainer(reactElement, domElement._rootContainer, null, callback); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment