Skip to content

Commit f3549a9

Browse files
committed
refactor: initial redux store & useRestate
1 parent db6bf0c commit f3549a9

8 files changed

Lines changed: 124 additions & 29 deletions

File tree

example/src/App.tsx

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
1-
import React, { Component } from 'react';
1+
import React from 'react';
2+
import { useRestate } from '../..';
23

3-
class App extends Component {
4-
public render() {
5-
return (
6-
<div className="App">
7-
<header className="App-header">
8-
<p>
9-
Edit <code>src/App.tsx</code> and save to reload.
10-
</p>
11-
<a
12-
className="App-link"
13-
href="https://reactjs.org"
14-
target="_blank"
15-
rel="noopener noreferrer"
16-
>
17-
Learn React
18-
</a>
19-
</header>
20-
</div>
21-
);
22-
}
23-
}
4+
export default function Component() {
5+
const [restate, dispatch] = useRestate((state: any) => {
6+
return { count: state.count };
7+
});
248

25-
export default App;
9+
return (
10+
<div>
11+
<p>Hey</p>
12+
</div>
13+
);
14+
}

example/src/index.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
3+
import { createStore } from 'redux';
34
import './index.css';
45
import App from './App';
56
import * as serviceWorker from './serviceWorker';
7+
import { RestateProvider } from '../../';
8+
const Actions = {
9+
TOGGLE_MENU: 'TOGGLE_MENU',
10+
};
611

7-
ReactDOM.render(<App />, document.getElementById('root'));
12+
const Reducer = (state = {}, action: any) => {
13+
switch (action.type) {
14+
case Actions.TOGGLE_MENU:
15+
break;
16+
default:
17+
return state;
18+
}
19+
return state;
20+
};
21+
22+
const store = createStore(Reducer);
23+
24+
ReactDOM.render(
25+
<RestateProvider value={store}>
26+
<App />
27+
</RestateProvider>,
28+
document.getElementById('root'),
29+
);
830

931
serviceWorker.register();

example/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"compilerOptions": {
3-
"target": "es5",
3+
"lib": ["es2016", "dom"],
4+
"target": "es6",
45
"allowJs": true,
56
"skipLibCheck": false,
67
"esModuleInterop": true,

index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React, { useState, useContext, useEffect, useRef } from 'react';
2+
import { Action, Dispatch, Store } from 'redux';
3+
4+
const RestateContext: React.Context<Store<any> | null> = React.createContext(null);
5+
6+
export const RestateProvider = RestateContext.Provider;
7+
8+
export function useRestate<T, U>(selector: any) {
9+
const store = useContext(RestateContext);
10+
const restate: T = null;
11+
console.log(store, selector);
12+
13+
return [restate, store.dispatch];
14+
}

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
"repository": "https://github.com/animify/useRestate.git",
66
"author": "Stefan Mansson <st.mansson@icloud.com>",
77
"license": "MIT",
8-
"dependencies": {},
8+
"dependencies": {
9+
"react": "^16.7.0-alpha.2",
10+
"redux": "^4.0.1"
11+
},
912
"scripts": {
1013
"release": "changelog && git push --follow-tags && yarn publish --access public --non-interactive",
1114
"lint": "tslint -p tsconfig.json"
1215
},
1316
"devDependencies": {
17+
"@types/react": "^16.7.11",
1418
"changelog.md": "^1.1.0",
1519
"husky": "^1.2.0",
1620
"lint-staged": "^8.1.0",

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"compilerOptions": {
33
"module": "commonjs",
44
"lib": ["es2016", "dom"],
5+
"allowSyntheticDefaultImports": true,
56
"preserveConstEnums": true,
67
"removeComments": true,
78
"moduleResolution": "node",

tslint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"no-angle-bracket-type-assertion": true,
1111
"curly": [true, "ignore-same-line"],
1212
"variable-name": [
13-
true,
13+
false,
1414
"ban-keywords",
1515
"check-format",
1616
"allow-leading-underscore"

yarn.lock

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@
2323
dependencies:
2424
any-observable "^0.3.0"
2525

26+
"@types/prop-types@*":
27+
version "15.5.6"
28+
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.5.6.tgz#9c03d3fed70a8d517c191b7734da2879b50ca26c"
29+
integrity sha512-ZBFR7TROLVzCkswA3Fmqq+IIJt62/T7aY/Dmz+QkU7CaW2QFqAitCE8Ups7IzmGhcN1YWMBT4Qcoc07jU9hOJQ==
30+
31+
"@types/react@^16.7.11":
32+
version "16.7.11"
33+
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.7.11.tgz#1743b82ea6a0c659467f0c7317c233edb1f10be9"
34+
integrity sha512-KZvX2XjzBPz+Dh7cjgDzzLxUmg4k8CnCSsB1V2Vtt1YtSIGe1sfHp2+Htl3S5DzeKrHnioaDHTMN/fjAvnEsSg==
35+
dependencies:
36+
"@types/prop-types" "*"
37+
csstype "^2.2.0"
38+
2639
ansi-align@^1.1.0:
2740
version "1.1.0"
2841
resolved "https://registry.yarnpkg.com/ansi-align/-/ansi-align-1.1.0.tgz#2f0c1658829739add5ebb15e6b0c6e3423f016ba"
@@ -514,6 +527,11 @@ cross-spawn@^6.0.0:
514527
shebang-command "^1.2.0"
515528
which "^1.2.9"
516529

530+
csstype@^2.2.0:
531+
version "2.5.7"
532+
resolved "https://registry.yarnpkg.com/csstype/-/csstype-2.5.7.tgz#bf9235d5872141eccfb2d16d82993c6b149179ff"
533+
integrity sha512-Nt5VDyOTIIV4/nRFswoCKps1R5CD1hkiyjBE9/thNaNZILLEviVw9yWQw15+O+CpNjQKB/uvdcxFFOrSflY3Yw==
534+
517535
currently-unhandled@^0.4.1:
518536
version "0.4.1"
519537
resolved "https://registry.yarnpkg.com/currently-unhandled/-/currently-unhandled-0.4.1.tgz#988df33feab191ef799a61369dd76c17adf957ea"
@@ -1340,6 +1358,11 @@ jest-validate@^23.5.0:
13401358
leven "^2.1.0"
13411359
pretty-format "^23.6.0"
13421360

1361+
"js-tokens@^3.0.0 || ^4.0.0":
1362+
version "4.0.0"
1363+
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
1364+
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==
1365+
13431366
js-tokens@^3.0.2:
13441367
version "3.0.2"
13451368
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b"
@@ -1539,6 +1562,13 @@ log-update@^2.3.0:
15391562
cli-cursor "^2.0.0"
15401563
wrap-ansi "^3.0.1"
15411564

1565+
loose-envify@^1.1.0, loose-envify@^1.3.1, loose-envify@^1.4.0:
1566+
version "1.4.0"
1567+
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
1568+
integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==
1569+
dependencies:
1570+
js-tokens "^3.0.0 || ^4.0.0"
1571+
15421572
loud-rejection@^1.0.0:
15431573
version "1.6.0"
15441574
resolved "https://registry.yarnpkg.com/loud-rejection/-/loud-rejection-1.6.0.tgz#5b46f80147edee578870f086d04821cf998e551f"
@@ -1767,7 +1797,7 @@ number-is-nan@^1.0.0:
17671797
resolved "https://registry.yarnpkg.com/number-is-nan/-/number-is-nan-1.0.1.tgz#097b602b53422a522c1afb8790318336941a011d"
17681798
integrity sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=
17691799

1770-
object-assign@^4.0.1, object-assign@^4.1.0:
1800+
object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1:
17711801
version "4.1.1"
17721802
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
17731803
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=
@@ -2045,6 +2075,14 @@ process-nextick-args@~2.0.0:
20452075
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.0.tgz#a37d732f4271b4ab1ad070d35508e8290788ffaa"
20462076
integrity sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw==
20472077

2078+
prop-types@^15.6.2:
2079+
version "15.6.2"
2080+
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.6.2.tgz#05d5ca77b4453e985d60fc7ff8c859094a497102"
2081+
integrity sha512-3pboPvLiWD7dkI3qf3KbUe6hKFKa52w+AE0VCqECtf+QHAKgOL37tTaNCnuX1nAAQ4ZhyP+kYVKf8rLmJ/feDQ==
2082+
dependencies:
2083+
loose-envify "^1.3.1"
2084+
object-assign "^4.1.1"
2085+
20482086
protocols@^1.1.0, protocols@^1.4.0:
20492087
version "1.4.6"
20502088
resolved "https://registry.yarnpkg.com/protocols/-/protocols-1.4.6.tgz#f8bb263ea1b5fd7a7604d26b8be39bd77678bf8a"
@@ -2086,6 +2124,16 @@ rc@^1.0.1, rc@^1.1.6:
20862124
minimist "^1.2.0"
20872125
strip-json-comments "~2.0.1"
20882126

2127+
react@^16.7.0-alpha.2:
2128+
version "16.7.0-alpha.2"
2129+
resolved "https://registry.yarnpkg.com/react/-/react-16.7.0-alpha.2.tgz#924f2ae843a46ea82d104a8def7a599fbf2c78ce"
2130+
integrity sha512-Xh1CC8KkqIojhC+LFXd21jxlVtzoVYdGnQAi/I2+dxbmos9ghbx5TQf9/nDxc4WxaFfUQJkya0w1k6rMeyIaxQ==
2131+
dependencies:
2132+
loose-envify "^1.1.0"
2133+
object-assign "^4.1.1"
2134+
prop-types "^15.6.2"
2135+
scheduler "^0.12.0-alpha.2"
2136+
20892137
read-all-stream@^3.0.0:
20902138
version "3.1.0"
20912139
resolved "https://registry.yarnpkg.com/read-all-stream/-/read-all-stream-3.1.0.tgz#35c3e177f2078ef789ee4bfafa4373074eaef4fa"
@@ -2158,6 +2206,14 @@ redent@^2.0.0:
21582206
indent-string "^3.0.0"
21592207
strip-indent "^2.0.0"
21602208

2209+
redux@^4.0.1:
2210+
version "4.0.1"
2211+
resolved "https://registry.yarnpkg.com/redux/-/redux-4.0.1.tgz#436cae6cc40fbe4727689d7c8fae44808f1bfef5"
2212+
integrity sha512-R7bAtSkk7nY6O/OYMVR9RiBI+XghjF9rlbl5806HJbQph0LJVHZrU5oaO4q70eUKiqMRqm4y07KLTlMZ2BlVmg==
2213+
dependencies:
2214+
loose-envify "^1.4.0"
2215+
symbol-observable "^1.2.0"
2216+
21612217
regex-not@^1.0.0, regex-not@^1.0.2:
21622218
version "1.0.2"
21632219
resolved "https://registry.yarnpkg.com/regex-not/-/regex-not-1.0.2.tgz#1f4ece27e00b0b65e0247a6810e6a85d83a5752c"
@@ -2279,6 +2335,14 @@ safe-regex@^1.1.0:
22792335
dependencies:
22802336
ret "~0.1.10"
22812337

2338+
scheduler@^0.12.0-alpha.2:
2339+
version "0.12.0-alpha.2"
2340+
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.12.0-alpha.2.tgz#2a8bc8dc6ecdb75fa6480ceeedc1f187c9539970"
2341+
integrity sha512-bfqFzGH18MjjhePIzYQNR0uGQ1wMCX6Q83c2s+3fzyuqKT6zBI2wNQTpq01q72C7QItAp8if5w2LfMiXnI2SYw==
2342+
dependencies:
2343+
loose-envify "^1.1.0"
2344+
object-assign "^4.1.1"
2345+
22822346
semver-compare@^1.0.0:
22832347
version "1.0.0"
22842348
resolved "https://registry.yarnpkg.com/semver-compare/-/semver-compare-1.0.0.tgz#0dee216a1c941ab37e9efb1788f6afc5ff5537fc"
@@ -2600,7 +2664,7 @@ supports-color@^5.3.0:
26002664
dependencies:
26012665
has-flag "^3.0.0"
26022666

2603-
symbol-observable@^1.1.0:
2667+
symbol-observable@^1.1.0, symbol-observable@^1.2.0:
26042668
version "1.2.0"
26052669
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.2.0.tgz#c22688aed4eab3cdc2dfeacbb561660560a00804"
26062670
integrity sha512-e900nM8RRtGhlV36KGEU9k65K3mPb1WV70OdjfxlG2EAuM1noi/E/BaW/uMhL7bPEssK8QV57vN3esixjUvcXQ==

0 commit comments

Comments
 (0)