Created
May 10, 2017 07:06
-
-
Save tblaisot/70bfea50d368d219da2358ba31bef5c7 to your computer and use it in GitHub Desktop.
Monkey patching of React to automagicly use functionnal components as functions
This file contains 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
/* | |
Monkey patching of React to automagicly use functionnal components as functions for a real gain in perforance | |
See article "https://medium.com/missive-app/45-faster-react-functional-components-now-3509a668e69f" | |
WARNING: do not use if you don't understand what you are doing and the conséquences of such patching | |
*/ | |
function patchReactForFunctionnalComponent(React) { | |
React.Component.prototype.__is_react_component__ = true; | |
var ReactcreateElement = React.createElement; | |
React.createElement = function (type, config, children) { | |
var args = []; | |
for (var i = 0; i < arguments.length; i++) { | |
args.push(arguments[i]); | |
} | |
if (args.length === 1) { | |
args.push({}); | |
} | |
if (args[1] === null) { | |
args[1] = {}; | |
} | |
if (typeof type === "function" && !args[0].prototype.__is_react_component__ && !args[0].contextTypes) { | |
// optimisation stateless component react | |
if (args.length === 3) { | |
args[1].children = args[2]; | |
} | |
return args[0](args[1]); | |
} else { | |
// Composants type React.createClass ou React.Component ou stateless avec contexttype | |
return ReactcreateElement.apply(ReactcreateElement, args); | |
} | |
}; | |
var ReactcreateClass = React.createClass; | |
React.createClass = function (specification) { | |
var createdClass = ReactcreateClass(specification); | |
createdClass.prototype.__is_react_component__ = true; | |
return createdClass | |
}; | |
} | |
window.patchReactFunctionnalComponents = patchReactFunctionnalComponents; | |
if (window.React !== undefined && window.React !== null) { | |
window.patchReactFunctionnalComponents(window.React); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment