Last active
March 2, 2017 01:49
-
-
Save KevinDoughty/96c1bb38aebfe59ab0a680842a5bf2fc to your computer and use it in GitHub Desktop.
React Abomination
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
function abomination(InnerComponent) { | |
const Subclass = (function(SuperComponent) { | |
function Subclass() { | |
SuperComponent.prototype.constructor.apply(this,arguments); | |
} | |
Subclass.prototype = Object.create(SuperComponent.prototype); | |
Subclass.prototype.componentDidMount = function() { | |
if (SuperComponent.prototype.componentDidMount) { | |
SuperComponent.prototype.componentDidMount.apply(this,arguments); | |
} | |
}; | |
Subclass.prototype.render = function render() { | |
const resultElement = SuperComponent.prototype.render.call(this); | |
const output = Object.assign({},resultElement.props); | |
output.style = Object.assign({},resultElement.props.style, { | |
color:"red" | |
}); | |
return React.cloneElement(resultElement,output); | |
}; | |
return Subclass; | |
})(InnerComponent); | |
const EvilClass = React.createClass({ | |
childInstance: null, | |
render: function() { | |
const owner = this; | |
const reference = function(component) { | |
if (component && owner.childInstance !== component) { | |
owner.childInstance = component; | |
} | |
}; | |
const output = Object.assign({},this.props) | |
output.ref = reference; | |
output.message = "bad"; | |
const result = React.createElement(Subclass,output); | |
return result; | |
} | |
}); | |
return EvilClass; | |
} | |
const AppClass = abomination( React.createClass({ | |
render: function() { | |
const message = "This is " + this.props.message; | |
return React.DOM.div({ | |
style:{ | |
color:"green" | |
} | |
}, message); | |
} | |
})); | |
const App = React.createFactory(AppClass); | |
ReactDOM.render(App({ | |
message:"good" | |
}), document.getElementById("root")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment