Skip to content

Commit 05920fe

Browse files
committed
feat: export useDispatch, useSelector and useStore from [email protected]
1 parent 3f1f90e commit 05920fe

File tree

4 files changed

+69
-15
lines changed

4 files changed

+69
-15
lines changed

packages/dva/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"history": "^4.7.2",
4040
"invariant": "^2.2.4",
4141
"isomorphic-fetch": "^2.2.1",
42-
"react-redux": "^7.0.1",
42+
"react-redux": "^7.1.0",
4343
"react-router-dom": "^4.3.1",
4444
"redux": "^4.0.1"
4545
},

packages/dva/src/index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import React from 'react';
22
import invariant from 'invariant';
33
import { createBrowserHistory, createMemoryHistory, createHashHistory } from 'history';
44
import document from 'global/document';
5-
import { Provider, connect, connectAdvanced } from 'react-redux';
5+
import {
6+
Provider,
7+
connect,
8+
connectAdvanced,
9+
useSelector,
10+
useDispatch,
11+
useStore,
12+
} from 'react-redux';
613
import { utils, create, saga } from 'dva-core';
714
import * as router from 'react-router-dom';
815
import * as routerRedux from 'connected-react-router';
@@ -105,7 +112,7 @@ function patchHistory(history) {
105112

106113
export fetch from 'isomorphic-fetch';
107114
export dynamic from './dynamic';
108-
export { connect, connectAdvanced };
115+
export { connect, connectAdvanced, useSelector, useDispatch, useStore };
109116
export { router };
110117
export { saga };
111118
export { routerRedux };

packages/dva/test/index.e2e.js

+50-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import React from 'react';
2-
import { render, fireEvent } from 'react-testing-library';
3-
import dva, { connect, createMemoryHistory, router, routerRedux } from '../dist/index';
2+
import { render, fireEvent, cleanup } from 'react-testing-library';
3+
import dva, {
4+
connect,
5+
useDispatch,
6+
useSelector,
7+
useStore,
8+
createMemoryHistory,
9+
router,
10+
routerRedux,
11+
} from '../dist/index';
412

513
const { Link, Switch, Route, Router } = router;
614

15+
afterEach(cleanup);
16+
717
test('normal', () => {
818
const app = dva();
919
app.model({
@@ -49,14 +59,51 @@ test('connect', () => {
4959
);
5060
});
5161
app.router(() => <App />);
52-
app.start();
5362

5463
const { getByTestId, getByText } = render(React.createElement(app.start()));
5564
expect(getByTestId('count').innerHTML).toEqual('0');
5665
fireEvent.click(getByText('add'));
5766
expect(getByTestId('count').innerHTML).toEqual('1');
5867
});
5968

69+
test('hooks api: useDispatch, useSelector and useStore', () => {
70+
const app = dva();
71+
app.model({
72+
namespace: 'count',
73+
state: 0,
74+
reducers: {
75+
add(state) {
76+
return state + 1;
77+
},
78+
},
79+
});
80+
const App = () => {
81+
const dispatch = useDispatch();
82+
const store = useStore();
83+
const { count } = useSelector(state => ({ count: state.count }));
84+
return (
85+
<>
86+
<div data-testid="count">{count}</div>
87+
<div data-testid="state">{store.getState().count}</div>
88+
<button
89+
onClick={() => {
90+
dispatch({ type: 'count/add' });
91+
}}
92+
>
93+
add
94+
</button>
95+
</>
96+
);
97+
};
98+
app.router(() => <App />);
99+
100+
const { getByTestId, getByText } = render(React.createElement(app.start()));
101+
expect(getByTestId('count').innerHTML).toEqual('0');
102+
fireEvent.click(getByText('add'));
103+
expect(getByTestId('count').innerHTML).toEqual('1');
104+
expect(getByTestId('state').innerHTML).toEqual('1');
105+
});
106+
60107
test('navigate', async () => {
61108
const history = createMemoryHistory({
62109
initialEntries: ['/'],

packages/dva/yarn.lock

+9-9
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
dependencies:
1010
regenerator-runtime "^0.12.0"
1111

12-
"@babel/runtime@^7.4.3":
13-
version "7.4.3"
14-
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.4.3.tgz#79888e452034223ad9609187a0ad1fe0d2ad4bdc"
15-
integrity sha512-9lsJwJLxDh/T3Q3SZszfWOTkk3pHbkmH+3KY+zwIDmsNlxsumuhS2TH3NIpktU4kNvfzy+k3eLT7aTJSPTo0OA==
12+
"@babel/runtime@^7.4.5":
13+
version "7.4.5"
14+
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.4.5.tgz#582bb531f5f9dc67d2fcb682979894f75e253f12"
15+
integrity sha512-TuI4qpWZP6lGOGIuGWtp9sPluqYICmbk8T/1vpSysqJxRPkudh/ofFWyqdcMsDf2s7KvDL4/YHgKyvcS3g9CJQ==
1616
dependencies:
1717
regenerator-runtime "^0.13.2"
1818

@@ -212,12 +212,12 @@ react-is@^16.8.6:
212212
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.8.6.tgz#5bbc1e2d29141c9fbdfed456343fe2bc430a6a16"
213213
integrity sha512-aUk3bHfZ2bRSVFFbbeVS4i+lNPZr3/WM5jT2J5omUVV1zzcs1nAaf3l51ctA5FFvCRbhrH0bdAsRRQddFJZPtA==
214214

215-
react-redux@^7.0.1:
216-
version "7.0.1"
217-
resolved "https://registry.yarnpkg.com/react-redux/-/react-redux-7.0.1.tgz#321285e6c85c3586d11cc066ab33dc580da599fb"
218-
integrity sha512-orSiI/QXtGiiJmf8lN/zVTx4hysFo/kGOsce28IUu/mu98AGemBwPTDzf64P4Vf/miRmevO8/w2RSw2awDd21w==
215+
react-redux@^7.1.0:
216+
version "7.1.0"
217+
resolved "https://registry.yarnpkg.com/react-redux/-/react-redux-7.1.0.tgz#72af7cf490a74acdc516ea9c1dd80e25af9ea0b2"
218+
integrity sha512-hyu/PoFK3vZgdLTg9ozbt7WF3GgX5+Yn3pZm5/96/o4UueXA+zj08aiSC9Mfj2WtD1bvpIb3C5yvskzZySzzaw==
219219
dependencies:
220-
"@babel/runtime" "^7.4.3"
220+
"@babel/runtime" "^7.4.5"
221221
hoist-non-react-statics "^3.3.0"
222222
invariant "^2.2.4"
223223
loose-envify "^1.4.0"

0 commit comments

Comments
 (0)