Skip to content

Commit 0361a29

Browse files
v0.1.0 code dump
1 parent 005d3f5 commit 0361a29

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed

.babelrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"presets": [
3+
["es2015", {"modules": "commonjs"}],
4+
"react"
5+
]
6+
}

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
.DS_Store
3+
.idea/*
4+
*.log
5+
dist
6+

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,53 @@
11
# react-stateful-hoc
2+
23
Higher Order Component for demoing stateless functional React components
4+
5+
## Usage
6+
7+
> npm i -D react-stateful-hoc
8+
9+
```
10+
/** components/Clicker */
11+
12+
import React from 'react';
13+
import propTypes from 'prop-types';
14+
15+
16+
const Clicker = (props) => {
17+
const { value } = props; // event handlers will need to reference props directly to maintain scope
18+
return <div onClick={() => props.onClick(1)}>CLICK ME: {value}</div>;
19+
};
20+
21+
Clicker.propTypes = {
22+
value: PropTypes.number.isRequired,
23+
onClick: PropTypes.func.isRequired
24+
};
25+
26+
Clicker.defaultProps = {
27+
value: 0
28+
};
29+
30+
function onClickHandler(state, payload) {
31+
return state += payload;
32+
}
33+
34+
export { Clicker, onClickHandler };
35+
```
36+
37+
38+
```
39+
/** App */
40+
41+
import React from 'react';
42+
import StatefulHOC from 'react-stateful-hoc';
43+
import { Clicker, onClickHandler } from 'components/Clicker';
44+
45+
46+
const ClickerMock = StatefulHOC(Clicker);
47+
48+
const ClickerDemo = ClickerMock({
49+
onClick(prop) {
50+
this.setState({ value: onClickHandler(this.value, prop) });
51+
},
52+
});
53+
```

package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "react-stateful-hoc",
3+
"description": "Higher Order Component for demoing stateless functional React components",
4+
"version": "0.1.0",
5+
"license": "MIT",
6+
"author": "Patrick Marabeas <[email protected]> (http://github.com/patrickmarabeas)",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/patrickmarabeas/react-stateful-hoc.git"
10+
},
11+
"bugs": {
12+
"url": "https://github.com/patrickmarabeas/react-stateful-hoc/issues"
13+
},
14+
"main": "dist/index.js",
15+
"scripts": {
16+
"build": "./node_modules/.bin/babel src --out-dir dist",
17+
"prepublish": "npm run build"
18+
},
19+
"dependencies": {
20+
"react": "^15.5.4"
21+
},
22+
"devDependencies": {
23+
"babel-cli": "^6.24.1",
24+
"babel-preset-es2015": "^6.24.1",
25+
"babel-preset-react": "^6.24.1"
26+
}
27+
}

src/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
3+
export default StatelessComponent => (initialState = {}) => class extends React.Component {
4+
constructor(props) {
5+
super(props);
6+
this.state = typeof initialState === 'function' ? initialState(props) : initialState;
7+
this.setState = this.setState.bind(this);
8+
}
9+
render() {
10+
const { state, props, setState } = this;
11+
return <StatelessComponent {...props} {...state} setState={setState} />;
12+
}
13+
};

0 commit comments

Comments
 (0)