1.0.2 • Published 7 years ago
react-statics v1.0.2
react-statics
Decorate React components with static properties
Table of contents
Summary
When working with stateless functional components, or overriding default values on decorated components, applying statics can feel a bit boilerplate-y. This decorator centralizes the static property assignment, and allows for easy decorated composition.
Usage
Standard
import React from "react";
import statics from "react-statics";
const App = ({ foo }, { bar }) => (
<div>
{foo}: {bar}
</div>
);
export default statics({
contextTypes: {
bar: PropTypes.node.isRequired
},
displayName: "MySpecialApp",
propTypes: {
foo: PropTypes.string.isRequired
}
})(App);Composed
import React from "react";
import { translate } from "react-i18next";
import { connect } from "react-redux";
import statics from "react-statics";
import { compose } from "redux";
const App = ({ foo }, { bar }) => (
<div>
{foo}: {bar}
</div>
);
export default compose(
statics({
contextTypes: {
bar: PropTypes.node.isRequired
},
displayName: "MySpecialApp",
propTypes: {
foo: PropTypes.string.isRequired
}
}),
translate(["namespace"]),
connect(({ reducer }) => reducer)
)(App);NOTE: To ensure the values passed override any assigned by other composed decorators, place statics last to execute in the composition chain.
Development
Standard stuff, clone the repo and npm install dependencies. The npm scripts available:
build=> run rollup to build developmentdistfilesclean=> runclean:dist,clean:es, andclean:libclean:dist=> remove all existing files in thedistfolderclean:es=> remove all existing files in theesfolderclean:lib=> remove all existing files in thelibfolderdev=> run webpack dev server to run example app / playgrounddist=> runsclean:distandbuildlint=> run ESLint against all files in thesrcfolderlint:fix=> run ESLint against all files in thesrcfolder, fixing anything it can automaticallyprepublish=> runsprepublish:compilewhen publishingprepublish:compile=> runlint,test:coverage,transpile:lib,transpile:es, anddisttest=> run AVA test functions withNODE_ENV=testtest:coverage=> runtestbut withnycfor coverage checkertest:watch=> runtest, but with persistent watchertranspile:lib=> run babel against all files insrcto create files inlibtranspile:es=> run babel against all files insrcto create files ines, preserving ES2015 modules (forpkg.module)