From f3549a93aeee12210855c751b7fdaaccc5b5dba4 Mon Sep 17 00:00:00 2001 From: Stefan Mansson Date: Sat, 1 Dec 2018 21:35:45 +0200 Subject: [PATCH] refactor: initial redux store & useRestate --- example/src/App.tsx | 35 ++++++++-------------- example/src/index.tsx | 24 ++++++++++++++- example/tsconfig.json | 3 +- index.ts | 14 +++++++++ package.json | 6 +++- tsconfig.json | 1 + tslint.json | 2 +- yarn.lock | 68 +++++++++++++++++++++++++++++++++++++++++-- 8 files changed, 124 insertions(+), 29 deletions(-) create mode 100644 index.ts diff --git a/example/src/App.tsx b/example/src/App.tsx index d57e51c..27a949e 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -1,25 +1,14 @@ -import React, { Component } from 'react'; +import React from 'react'; +import { useRestate } from '../..'; -class App extends Component { - public render() { - return ( -
-
-

- Edit src/App.tsx and save to reload. -

- - Learn React - -
-
- ); - } -} +export default function Component() { + const [restate, dispatch] = useRestate((state: any) => { + return { count: state.count }; + }); -export default App; + return ( +
+

Hey

+
+ ); +} diff --git a/example/src/index.tsx b/example/src/index.tsx index c70c2ea..7cd92e0 100644 --- a/example/src/index.tsx +++ b/example/src/index.tsx @@ -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(, 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( + + + , + document.getElementById('root'), +); serviceWorker.register(); diff --git a/example/tsconfig.json b/example/tsconfig.json index f44f316..dbd465b 100644 --- a/example/tsconfig.json +++ b/example/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { - "target": "es5", + "lib": ["es2016", "dom"], + "target": "es6", "allowJs": true, "skipLibCheck": false, "esModuleInterop": true, diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..78eab8c --- /dev/null +++ b/index.ts @@ -0,0 +1,14 @@ +import React, { useState, useContext, useEffect, useRef } from 'react'; +import { Action, Dispatch, Store } from 'redux'; + +const RestateContext: React.Context | null> = React.createContext(null); + +export const RestateProvider = RestateContext.Provider; + +export function useRestate(selector: any) { + const store = useContext(RestateContext); + const restate: T = null; + console.log(store, selector); + + return [restate, store.dispatch]; +} diff --git a/package.json b/package.json index 4ecce4d..f9b68a3 100644 --- a/package.json +++ b/package.json @@ -5,12 +5,16 @@ "repository": "https://github.com/animify/useRestate.git", "author": "Stefan Mansson ", "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", diff --git a/tsconfig.json b/tsconfig.json index 582bf16..8fcb844 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,6 +2,7 @@ "compilerOptions": { "module": "commonjs", "lib": ["es2016", "dom"], + "allowSyntheticDefaultImports": true, "preserveConstEnums": true, "removeComments": true, "moduleResolution": "node", diff --git a/tslint.json b/tslint.json index 36eb394..b16aad4 100644 --- a/tslint.json +++ b/tslint.json @@ -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" diff --git a/yarn.lock b/yarn.lock index 11c3bb0..60fb823 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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" @@ -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" @@ -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= @@ -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" @@ -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" @@ -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" @@ -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" @@ -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==