Skip to content

Commit ffe6ffe

Browse files
committed
refactor: remove useStore function & error check
1 parent 8394317 commit ffe6ffe

5 files changed

Lines changed: 45 additions & 26 deletions

File tree

example/App.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { createStore } from 'redux';
3-
import Comp from './Comp';
4-
import { RestateProvider } from '../src';
3+
import Count from './Count';
4+
import { RestateProvider } from 'use-restate';
55

66
const Actions = {
77
INCREMENT: 'INCREMENT',
@@ -28,7 +28,7 @@ const store = createStore(Reducer, { count: 3 });
2828
export default function App() {
2929
return (
3030
<RestateProvider value={store}>
31-
<Comp />
31+
<Count />
3232
</RestateProvider>
3333
);
3434
}

example/Count.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from 'react';
2+
import { useRestate, useAction, useDispatch } from 'use-restate';
3+
4+
export default function Component() {
5+
const dispatch = useDispatch();
6+
const { count } = useRestate((state: { count: number }) => {
7+
return { count: state.count };
8+
});
9+
10+
const incrementAction = { type: 'INCREMENT' };
11+
const derementAction = { type: 'DECREMENT' };
12+
const increment = useAction(incrementAction);
13+
14+
return (
15+
<div>
16+
<p>{count}</p>
17+
<a onClick={increment}>Increment count</a>
18+
<a onClick={() => dispatch(derementAction)}>Decrement count</a>
19+
</div>
20+
);
21+
}

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
"test": "jest"
2626
},
2727
"dependencies": {
28-
"react": "^16.7.0-alpha.2",
29-
"react-dom": "^16.7.0-alpha.2",
30-
"redux": "^4.0.1"
28+
"react": "^16.7.0-alpha.0",
29+
"react-dom": "^16.7.0-alpha.0",
30+
"redux": "^4.0.1",
31+
"use-restate": "^0.0.9"
3132
},
3233
"devDependencies": {
3334
"@types/jest": "^23.3.10",
@@ -62,4 +63,4 @@
6263
"lint-staged": {
6364
"*.ts": "yarn run lint"
6465
}
65-
}
66+
}

src/index.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,8 @@ import shallowEqual from './shallowEqual';
55
export const RestateContext: React.Context<Store<any> | undefined> = React.createContext(undefined);
66
export const RestateProvider = RestateContext.Provider;
77

8-
const useStore = () => {
9-
const store = useContext(RestateContext);
10-
11-
if (!store) {
12-
throw new Error(
13-
'use-restate requires you to initiate your redux-like store - e.g <RestateProvider value={store} />',
14-
);
15-
}
16-
17-
return store;
18-
};
19-
208
export function useRestate<TState, TSelector>(selectFrom: (state: TState) => TSelector): TSelector {
21-
const store = useStore();
9+
const store = useContext(RestateContext);
2210

2311
const [restate, setRestate] = useState(() => selectFrom(store.getState()));
2412
const previousRestate = useRef({});
@@ -51,7 +39,7 @@ export function useRestate<TState, TSelector>(selectFrom: (state: TState) => TSe
5139
}
5240

5341
export function useDispatch<TAction extends Action>(): Dispatch<TAction> {
54-
const store = useStore();
42+
const store = useContext(RestateContext);
5543

5644
return store.dispatch;
5745
}

yarn.lock

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7118,7 +7118,7 @@ rc@^1.0.1, rc@^1.1.6, rc@^1.2.7:
71187118
minimist "^1.2.0"
71197119
strip-json-comments "~2.0.1"
71207120

7121-
react-dom@^16.7.0-alpha.2:
7121+
react-dom@^16.7.0-alpha.0, react-dom@^16.7.0-alpha.2:
71227122
version "16.7.0-alpha.2"
71237123
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.7.0-alpha.2.tgz#16632880ed43676315991d8b412cce6975a30282"
71247124
integrity sha512-o0mMw8jBlwHjGZEy/vvKd/6giAX0+skREMOTs3/QHmgi+yAhUClp4My4Z9lsKy3SXV+03uPdm1l/QM7NTcGuMw==
@@ -7128,7 +7128,7 @@ react-dom@^16.7.0-alpha.2:
71287128
prop-types "^15.6.2"
71297129
scheduler "^0.12.0-alpha.2"
71307130

7131-
react@^16.7.0-alpha.2:
7131+
react@^16.7.0-alpha.0, react@^16.7.0-alpha.2:
71327132
version "16.7.0-alpha.2"
71337133
resolved "https://registry.yarnpkg.com/react/-/react-16.7.0-alpha.2.tgz#924f2ae843a46ea82d104a8def7a599fbf2c78ce"
71347134
integrity sha512-Xh1CC8KkqIojhC+LFXd21jxlVtzoVYdGnQAi/I2+dxbmos9ghbx5TQf9/nDxc4WxaFfUQJkya0w1k6rMeyIaxQ==
@@ -8495,9 +8495,9 @@ timsort@^0.3.0:
84958495
integrity sha1-QFQRqOfmM5/mTbmiNN4R3DHgK9Q=
84968496

84978497
tiny-glob@^0.2.0:
8498-
version "0.2.3"
8499-
resolved "https://registry.yarnpkg.com/tiny-glob/-/tiny-glob-0.2.3.tgz#9dba7fe22b1e4e2e7bb59815b10152bdf3d085b4"
8500-
integrity sha512-pkCxlClaL6MznxrgW4D2VDk5w6hz3SEOtXcJ9CPhxLjhpsvTd1g3qkMAqxoJNjKsCQlJj6w485ij5d5/Fb9jUQ==
8498+
version "0.2.4"
8499+
resolved "https://registry.yarnpkg.com/tiny-glob/-/tiny-glob-0.2.4.tgz#11cbdb455903053c885791cbd6e5458a91b9f355"
8500+
integrity sha512-Tej4Ftrtw9LVQCOiwG+15/sg1vmKG4l0PF3dRBtofyRwv3C0/gU0u3iRQd9YCUR1NPQT3kMB+3w9ecK0qigPRA==
85018501
dependencies:
85028502
globalyzer "^0.1.0"
85038503
globrex "^0.1.1"
@@ -8819,6 +8819,15 @@ url@^0.11.0:
88198819
punycode "1.3.2"
88208820
querystring "0.2.0"
88218821

8822+
use-restate@^0.0.9:
8823+
version "0.0.9"
8824+
resolved "https://registry.yarnpkg.com/use-restate/-/use-restate-0.0.9.tgz#cce76d72a7e88139b07bd7d730f4771465eed9ed"
8825+
integrity sha512-Ex+3GBKqkhc8RrpcxrA75nt7HvUvQwCux2R2YJX+S9KrcjtypapyH3jatWNcrc3IdUu3g28RAiJegu9smeVk0Q==
8826+
dependencies:
8827+
react "^16.7.0-alpha.2"
8828+
react-dom "^16.7.0-alpha.2"
8829+
redux "^4.0.1"
8830+
88228831
use@^3.1.0:
88238832
version "3.1.1"
88248833
resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f"

0 commit comments

Comments
 (0)