If you use React, Preact, or Inferno, but don't want to use the class
syntax because you've read articles by Eric Elliott and others that have lead you to favour object composition over class inheritance, then this utility is for you.
Annoyed that recompose was coupled to React and Incompose to Inferno, and wanting a solution for Preact, I set out to see how hard a de-coupled library would be. Instead of assuming one of the aforementioned libraries, the compose
function accepts the Base Component Class (React.Component
, InfernoComponent
, ...) and the hyperscript function (React.createElement
, Preact.h
, ...) as the first two parameters.
$ npm install --save classless-component
As this is in rapid development, and by no means stable, please refer to the test.js
file for working examples.
Previously, the following worked:
const Comp = compose(Component, Preact.h,
function componentWillMount () {
...
}
)
And the name of the function would be magically assigned to a new object. This caused issues when using UglifyJS to minify the code (as the function referance name was no longer guaranteed).
Instead, each param should be an explicit object with property names for render
, componentDidMount
, etc.
Based on preact-classless-component by Laurence Dorman.
MIT