Last active
May 11, 2021 16:18
-
-
Save agenthunt/11a05e991b361802326a2d50076fb13c to your computer and use it in GitHub Desktop.
hello-react-custom-renderer-hostConfig
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 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); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment