React.createElement( type, // DOM element or component [props], [...children] )
React.createElement( 'button', // or FancyButton class { className: 'button button-blue', children: ['Confirm'] // OPTIONAL! }, 'Confirm' // OPTIONAL! )
{ $$typeof: Symbol(react.element), type: "button", // or FancyButton key: null, ref: null, props: { className: ‘confirm-button’, children: ‘Confirm’ }
// …internalProperties
}
•JSX is a syntax extension for JS
•JSX is transpiled by Babel into…
React.createElement()
Transpiled into function call, which evaluates to JS object. That allows us:
to use expressions inside JSX with all of its benefits;
to use JSX as common expressions;
JSX prevents injection attacks. It is safe to use user inputs as all characters are escaped.
render() {
return (
<h1>Hello</h1>
<p>I won’t work!</p>
<h3>WHY?</h3>
);
}
React DOM is an API for interacting with various utilities. This API includes:
TL;DR
render() {
return <button onClick={() => this.setState({ liked: true })}>Like</button>
}
render() {
return React.createElement("button", {
onClick: () => this.setState({
liked: true
})
}, "Like");
}
const domContainer = document.querySelector('#like_button_container');
ReactDOM.render(<LikeButton/>, domContainer);
ReactDOM does not render components in the browser
ReactDOM.render(<LikeButton/>, domContainer);
ReactDOM does not render instances
ReactDOM does not render elements either
Let’s see through a wonderful metamorphose!
const e = React.createElement;
class LikeButton extends React.Component {
constructor(props) {
super(props);
this.state = { liked: false };
}
render() {
if (this.state.liked) {
return 'You liked this.';
}
return e(
'button',
{ onClick: () => this.setState({ liked: true }) },
'Like'
);
}
}
The first React element is the one we start with in the ReactDOM.render call
ReactDOM.render(<LikeButton/>, domContainer);
React now needs to answer the question: What is LikeButton?
ReacDOM.render(<LikeButton />, document.getElementByID(‘root))
ReactMount.render(<LikeButton />, document.getElementByID(‘root))
TopLevelWrapper.prototype.render = function () {
return this.props.child;
};
ReactDOM.render(<LikeButton/>, domContainer);
LikeButton.render()
createElement: createElementWithValidation()
export function createElementWithValidation(type, props, children) {
const validType = isValidElementType(type);
...
const element = createElement.apply(this, arguments);
function createElement(type, config, children) { return ReactElement(…);
CreateElement(type) -> ReactElement.type
Two calls to the createElement function — the first one with the type of the LikeButton class reference and the second one with the string button.
const nextElement = React.createElement( 'button', { onClick: () => this.setState({ liked: true }) }, 'Like')
// nextElement { $$typeof: Symbol(react.element), type: "button", key: null, ref: null, props: {…}, …} $$typeof: Symbol(react.element) key: null props: {onClick: ƒ, children: "Like"} ref: null type: "button" _owner: FiberNode {tag: 2, key: null, type: ƒ, stateNode: LikeButton, return: FiberNode, …} _store: {validated: false} __proto__: Object }
//componentInstance { context: {} props: {} refs: {} state: {liked: false} updater: {isMounted: ƒ, enqueueSetState: ƒ, enqueueReplaceState: ƒ, enqueueForceUpdate: ƒ} __proto__: Component }
function constructClassInstance(workInProgress, props, renderExpirationTime) {
var ctor = workInProgress.type;
var props = …
vaer context = …
var instance = new ctor(props, context);
}
// DOM element
{
innerHTML: "Like"
innerText: "Like“,
outerHTML: "<button></button>“,
tagName: "BUTTON"
textContent: "Like"
type: "submit“,…
__proto__: HTMLButtonElement
}
var domElement = createElement$1(type, props, rootContainerInstance, parentNamespace);
...
domElementWithChildren = finalizeInitialChildren(domElement, type, props, rootContainerInstance, hostContext)
AddEventListener Set textContent
function addEventBubbleListener(element, eventType, listener) { element.addEventListener(eventType, listener, false);
}
var setTextContent = function (node, text) {
…
node.textContent = text;
};
1.React.createElement – merge defaultProps into props, validate propTypes (for Components);
2.mount ReactDOMcomponent – HTML validation to ensure, that tags are nested correctly (for DOM elements);
3.Creating DOM elements -> assigning attributes and props -> rendering children -> mounting to DOM;
4.Assign updaters;
5.Calling the lifecycleHooks – componentWillMount() and componentDidMount();
6.Suppressing events to avoid focus / blur on reconciliation;
7.Caching scroll values;
…. AND MUCH MORE!