Skip to content

Commit 369b28b

Browse files
committed
added some helpers for migrating from react/redux to hyperapp and peer dependencies
1 parent 6bfc055 commit 369b28b

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
{
22
"name": "cra-hyperapp",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "Hyperapp expansion pack for create-react-app",
5+
"main": "src/index.js",
56
"bin": {
67
"hyperapp-scripts": "./bin/index.js"
78
},
89
"scripts": {
910
"format": "npx prettier --write **/*.js"
1011
},
12+
"peerDependencies": {
13+
"react-scripts": "^1.1.0",
14+
"hyperapp": "^1.0.2"
15+
},
1116
"repository": {
1217
"type": "git",
1318
"url": "git+https://github.com/okwolf/cra-hyperapp.git"

src/index.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { h as hh } from "hyperapp";
2+
3+
const propRename = name => (name.startsWith("on") ? name.toLowerCase() : name);
4+
export const h = (type, props, ...children) =>
5+
hh(
6+
type,
7+
typeof type === "function" && type.length === 1
8+
? { ...props, children }
9+
: Object.keys(props).reduce(
10+
(otherProps, name) => ({
11+
...otherProps,
12+
[propRename(name)]: props[name]
13+
}),
14+
{}
15+
),
16+
...children
17+
);
18+
19+
const secretInternals = (window.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_SERIOUSLY_IM_NOT_JOKING = {});
20+
21+
const makeInternalStore = ({ actions, reducer }) => ({
22+
getState: () => actions.getState(),
23+
dispatch: action => actions.setState(reducer(actions.getState(), action))
24+
});
25+
26+
export const withReducer = reducer => nextApp => (
27+
initialState,
28+
actionsTemplate,
29+
view,
30+
container
31+
) => {
32+
const reducerDefaultState = reducer(undefined, {});
33+
const actions = nextApp(
34+
{ ...reducerDefaultState, ...initialState },
35+
{
36+
...actionsTemplate,
37+
getState: () => state => state,
38+
setState: state => state
39+
},
40+
view,
41+
container
42+
);
43+
secretInternals.store = makeInternalStore({ actions, reducer });
44+
return actions;
45+
};
46+
47+
export const connect = (mapStateToProps, mapDispatchToProps) => Component => (
48+
props,
49+
children
50+
) =>
51+
h(
52+
Component,
53+
{
54+
...props,
55+
...mapStateToProps(secretInternals.store.getState(), props),
56+
...Object.keys(mapDispatchToProps).reduce(
57+
(otherActions, name) => ({
58+
...otherActions,
59+
[name]: data =>
60+
secretInternals.store.dispatch(mapDispatchToProps[name](data))
61+
}),
62+
{}
63+
)
64+
},
65+
children
66+
);

0 commit comments

Comments
 (0)