Skip to content

Commit

Permalink
refactor: initial redux store & useRestate
Browse files Browse the repository at this point in the history
  • Loading branch information
animify committed Dec 1, 2018
1 parent db6bf0c commit f3549a9
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 29 deletions.
35 changes: 12 additions & 23 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
import React, { Component } from 'react';
import React from 'react';
import { useRestate } from '../..';

class App extends Component {
public render() {
return (
<div className="App">
<header className="App-header">
<p>
Edit <code>src/App.tsx</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
}
export default function Component() {
const [restate, dispatch] = useRestate((state: any) => {
return { count: state.count };
});

export default App;
return (
<div>
<p>Hey</p>
</div>
);
}
24 changes: 23 additions & 1 deletion example/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { createStore } from 'redux';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { RestateProvider } from '../../';
const Actions = {
TOGGLE_MENU: 'TOGGLE_MENU',
};

ReactDOM.render(<App />, document.getElementById('root'));
const Reducer = (state = {}, action: any) => {
switch (action.type) {
case Actions.TOGGLE_MENU:
break;
default:
return state;
}
return state;
};

const store = createStore(Reducer);

ReactDOM.render(
<RestateProvider value={store}>
<App />
</RestateProvider>,
document.getElementById('root'),
);

serviceWorker.register();
3 changes: 2 additions & 1 deletion example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["es2016", "dom"],
"target": "es6",
"allowJs": true,
"skipLibCheck": false,
"esModuleInterop": true,
Expand Down
14 changes: 14 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React, { useState, useContext, useEffect, useRef } from 'react';
import { Action, Dispatch, Store } from 'redux';

const RestateContext: React.Context<Store<any> | null> = React.createContext(null);

export const RestateProvider = RestateContext.Provider;

export function useRestate<T, U>(selector: any) {
const store = useContext(RestateContext);
const restate: T = null;
console.log(store, selector);

return [restate, store.dispatch];
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
"repository": "https://github.com/animify/useRestate.git",
"author": "Stefan Mansson <[email protected]>",
"license": "MIT",
"dependencies": {},
"dependencies": {
"react": "^16.7.0-alpha.2",
"redux": "^4.0.1"
},
"scripts": {
"release": "changelog && git push --follow-tags && yarn publish --access public --non-interactive",
"lint": "tslint -p tsconfig.json"
},
"devDependencies": {
"@types/react": "^16.7.11",
"changelog.md": "^1.1.0",
"husky": "^1.2.0",
"lint-staged": "^8.1.0",
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"module": "commonjs",
"lib": ["es2016", "dom"],
"allowSyntheticDefaultImports": true,
"preserveConstEnums": true,
"removeComments": true,
"moduleResolution": "node",
Expand Down
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"no-angle-bracket-type-assertion": true,
"curly": [true, "ignore-same-line"],
"variable-name": [
true,
false,
"ban-keywords",
"check-format",
"allow-leading-underscore"
Expand Down
68 changes: 66 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@
dependencies:
any-observable "^0.3.0"

"@types/prop-types@*":
version "15.5.6"
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.5.6.tgz#9c03d3fed70a8d517c191b7734da2879b50ca26c"
integrity sha512-ZBFR7TROLVzCkswA3Fmqq+IIJt62/T7aY/Dmz+QkU7CaW2QFqAitCE8Ups7IzmGhcN1YWMBT4Qcoc07jU9hOJQ==

"@types/react@^16.7.11":
version "16.7.11"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.7.11.tgz#1743b82ea6a0c659467f0c7317c233edb1f10be9"
integrity sha512-KZvX2XjzBPz+Dh7cjgDzzLxUmg4k8CnCSsB1V2Vtt1YtSIGe1sfHp2+Htl3S5DzeKrHnioaDHTMN/fjAvnEsSg==
dependencies:
"@types/prop-types" "*"
csstype "^2.2.0"

ansi-align@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/ansi-align/-/ansi-align-1.1.0.tgz#2f0c1658829739add5ebb15e6b0c6e3423f016ba"
Expand Down Expand Up @@ -514,6 +527,11 @@ cross-spawn@^6.0.0:
shebang-command "^1.2.0"
which "^1.2.9"

csstype@^2.2.0:
version "2.5.7"
resolved "https://registry.yarnpkg.com/csstype/-/csstype-2.5.7.tgz#bf9235d5872141eccfb2d16d82993c6b149179ff"
integrity sha512-Nt5VDyOTIIV4/nRFswoCKps1R5CD1hkiyjBE9/thNaNZILLEviVw9yWQw15+O+CpNjQKB/uvdcxFFOrSflY3Yw==

currently-unhandled@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/currently-unhandled/-/currently-unhandled-0.4.1.tgz#988df33feab191ef799a61369dd76c17adf957ea"
Expand Down Expand Up @@ -1340,6 +1358,11 @@ jest-validate@^23.5.0:
leven "^2.1.0"
pretty-format "^23.6.0"

"js-tokens@^3.0.0 || ^4.0.0":
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==

js-tokens@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b"
Expand Down Expand Up @@ -1539,6 +1562,13 @@ log-update@^2.3.0:
cli-cursor "^2.0.0"
wrap-ansi "^3.0.1"

loose-envify@^1.1.0, loose-envify@^1.3.1, loose-envify@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==
dependencies:
js-tokens "^3.0.0 || ^4.0.0"

loud-rejection@^1.0.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/loud-rejection/-/loud-rejection-1.6.0.tgz#5b46f80147edee578870f086d04821cf998e551f"
Expand Down Expand Up @@ -1767,7 +1797,7 @@ number-is-nan@^1.0.0:
resolved "https://registry.yarnpkg.com/number-is-nan/-/number-is-nan-1.0.1.tgz#097b602b53422a522c1afb8790318336941a011d"
integrity sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=

object-assign@^4.0.1, object-assign@^4.1.0:
object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=
Expand Down Expand Up @@ -2045,6 +2075,14 @@ process-nextick-args@~2.0.0:
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.0.tgz#a37d732f4271b4ab1ad070d35508e8290788ffaa"
integrity sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw==

prop-types@^15.6.2:
version "15.6.2"
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.6.2.tgz#05d5ca77b4453e985d60fc7ff8c859094a497102"
integrity sha512-3pboPvLiWD7dkI3qf3KbUe6hKFKa52w+AE0VCqECtf+QHAKgOL37tTaNCnuX1nAAQ4ZhyP+kYVKf8rLmJ/feDQ==
dependencies:
loose-envify "^1.3.1"
object-assign "^4.1.1"

protocols@^1.1.0, protocols@^1.4.0:
version "1.4.6"
resolved "https://registry.yarnpkg.com/protocols/-/protocols-1.4.6.tgz#f8bb263ea1b5fd7a7604d26b8be39bd77678bf8a"
Expand Down Expand Up @@ -2086,6 +2124,16 @@ rc@^1.0.1, rc@^1.1.6:
minimist "^1.2.0"
strip-json-comments "~2.0.1"

react@^16.7.0-alpha.2:
version "16.7.0-alpha.2"
resolved "https://registry.yarnpkg.com/react/-/react-16.7.0-alpha.2.tgz#924f2ae843a46ea82d104a8def7a599fbf2c78ce"
integrity sha512-Xh1CC8KkqIojhC+LFXd21jxlVtzoVYdGnQAi/I2+dxbmos9ghbx5TQf9/nDxc4WxaFfUQJkya0w1k6rMeyIaxQ==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"
prop-types "^15.6.2"
scheduler "^0.12.0-alpha.2"

read-all-stream@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/read-all-stream/-/read-all-stream-3.1.0.tgz#35c3e177f2078ef789ee4bfafa4373074eaef4fa"
Expand Down Expand Up @@ -2158,6 +2206,14 @@ redent@^2.0.0:
indent-string "^3.0.0"
strip-indent "^2.0.0"

redux@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/redux/-/redux-4.0.1.tgz#436cae6cc40fbe4727689d7c8fae44808f1bfef5"
integrity sha512-R7bAtSkk7nY6O/OYMVR9RiBI+XghjF9rlbl5806HJbQph0LJVHZrU5oaO4q70eUKiqMRqm4y07KLTlMZ2BlVmg==
dependencies:
loose-envify "^1.4.0"
symbol-observable "^1.2.0"

regex-not@^1.0.0, regex-not@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/regex-not/-/regex-not-1.0.2.tgz#1f4ece27e00b0b65e0247a6810e6a85d83a5752c"
Expand Down Expand Up @@ -2279,6 +2335,14 @@ safe-regex@^1.1.0:
dependencies:
ret "~0.1.10"

scheduler@^0.12.0-alpha.2:
version "0.12.0-alpha.2"
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.12.0-alpha.2.tgz#2a8bc8dc6ecdb75fa6480ceeedc1f187c9539970"
integrity sha512-bfqFzGH18MjjhePIzYQNR0uGQ1wMCX6Q83c2s+3fzyuqKT6zBI2wNQTpq01q72C7QItAp8if5w2LfMiXnI2SYw==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"

semver-compare@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/semver-compare/-/semver-compare-1.0.0.tgz#0dee216a1c941ab37e9efb1788f6afc5ff5537fc"
Expand Down Expand Up @@ -2600,7 +2664,7 @@ supports-color@^5.3.0:
dependencies:
has-flag "^3.0.0"

symbol-observable@^1.1.0:
symbol-observable@^1.1.0, symbol-observable@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.2.0.tgz#c22688aed4eab3cdc2dfeacbb561660560a00804"
integrity sha512-e900nM8RRtGhlV36KGEU9k65K3mPb1WV70OdjfxlG2EAuM1noi/E/BaW/uMhL7bPEssK8QV57vN3esixjUvcXQ==
Expand Down

0 comments on commit f3549a9

Please sign in to comment.