Skip to content

Commit 09f9bdb

Browse files
committed
chore: 🤖 catch-up with master
2 parents a880724 + 8e11b7c commit 09f9bdb

9 files changed

+134
-86
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# [9.2.0](https://github.com/streamich/react-use/compare/v9.1.2...v9.2.0) (2019-05-31)
2+
3+
4+
### Features
5+
6+
* 🎸 improve useObservable() type annotations ([d0c3713](https://github.com/streamich/react-use/commit/d0c3713))
7+
* improve useSetState() typings ([fad4440](https://github.com/streamich/react-use/commit/fad4440))
8+
19
## [9.1.2](https://github.com/streamich/react-use/compare/v9.1.1...v9.1.2) (2019-05-24)
210

311

README.md

+16-27
Original file line numberDiff line numberDiff line change
@@ -123,36 +123,25 @@
123123
- [`useList`](./docs/useList.md) — tracks state of an array.
124124
- [`useMap`](./docs/useMap.md) — tracks state of an object.
125125

126-
## Usage
127126

128-
You need to have React [`16.8.0`](https://reactjs.org/blog/2019/02/06/react-v16.8.0.html) or later installed to use the Hooks API. You can import each hook individually
129-
130-
```js
131-
import useToggle from 'react-use/lib/useToggle'
132-
```
133-
134-
or use ES6 named imports
135-
136-
```js
137-
import {useToggle} from 'react-use'
138-
```
139-
140-
Depending on your bundler you might run into a missing dependency error with ES6 named import statements. Some hooks require you to install peer dependencies so we recommend using individual imports. If you want the best of both worlds you can transform the named import statements to individual import statements with [`babel-plugin-import`](https://github.com/ant-design/babel-plugin-import) by adding the following config to your `.babelrc` file:
141-
142-
```json
143-
[
144-
"import", {
145-
"libraryName": "react-use",
146-
"libraryDirectory": "lib",
147-
"camel2DashComponentName": false
148-
}
149-
]
150-
```
151-
152-
<h2 align="center"><sub>License</sub></h2>
127+
<br />
128+
<br />
129+
<br />
130+
<br />
131+
<br />
132+
<br />
133+
<br />
153134

154135
<p align="center">
155-
<a href="./LICENSE">Unlicense</a> &mdash; public domain.
136+
<a href="./docs/Usage.md"><strong>Usage</strong></a> &mdash; how to import.
137+
<br />
138+
<a href="./LICENSE"><strong>Unlicense</strong></a> &mdash; public domain.
156139
</p>
157140

141+
<br />
142+
<br />
143+
<br />
144+
<br />
145+
<br />
146+
158147
[img-demo]: https://img.shields.io/badge/demo-%20%20%20%F0%9F%9A%80-green.svg

babel.config.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,13 @@ module.exports = {
1010
],
1111
"@babel/preset-react",
1212
"@babel/preset-typescript"
13-
]
13+
],
14+
env: {
15+
test: {
16+
plugins: ['dynamic-import-node']
17+
},
18+
production: {
19+
plugins: ['@babel/plugin-syntax-dynamic-import']
20+
}
21+
}
1422
};

docs/Usage.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Usage
2+
3+
You need to have React [`16.8.0`](https://reactjs.org/blog/2019/02/06/react-v16.8.0.html) or later installed to use the Hooks API. You can import each hook individually
4+
5+
```js
6+
import useToggle from 'react-use/lib/useToggle'
7+
```
8+
9+
or use ES6 named imports
10+
11+
```js
12+
import {useToggle} from 'react-use'
13+
```
14+
15+
Depending on your bundler you might run into a missing dependency error with ES6 named import statements. Some hooks require you to install peer dependencies so we recommend using individual imports. If you want the best of both worlds you can transform the named import statements to individual import statements with [`babel-plugin-import`](https://github.com/ant-design/babel-plugin-import) by adding the following config to your `.babelrc` file:
16+
17+
```json
18+
[
19+
"import", {
20+
"libraryName": "react-use",
21+
"libraryDirectory": "lib",
22+
"camel2DashComponentName": false
23+
}
24+
]
25+
```

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "react-use",
3-
"version": "9.1.2",
3+
"version": "9.2.0",
44
"description": "Collection of React Hooks",
55
"main": "lib/index.js",
66
"module": "esm/index.js",
7+
"sideEffects": false,
78
"files": [
89
"lib/",
910
"esm/"
@@ -62,6 +63,8 @@
6263
"@babel/preset-env": "7.4.5",
6364
"@babel/preset-react": "7.0.0",
6465
"@babel/preset-typescript": "7.3.3",
66+
"@babel/plugin-syntax-dynamic-import": "7.2.0",
67+
"babel-plugin-dynamic-import-node": "^2.2.0",
6568
"@semantic-release/changelog": "3.0.2",
6669
"@semantic-release/git": "7.0.8",
6770
"@semantic-release/npm": "5.1.7",
@@ -97,7 +100,7 @@
97100
"tslint-eslint-rules": "5.4.0",
98101
"tslint-plugin-prettier": "2.0.1",
99102
"tslint-react": "4.0.0",
100-
"typescript": "3.4.5"
103+
"typescript": "3.5.1"
101104
},
102105
"config": {
103106
"commitizen": {

src/__tests__/useObservable.test.tsx

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { act, renderHook } from 'react-hooks-testing-library';
2+
import { Subject } from 'rxjs';
3+
import { useObservable } from '..';
4+
5+
test('default initial value is undefined', () => {
6+
const subject$ = new Subject();
7+
const { result } = renderHook(() => useObservable(subject$));
8+
9+
expect(result.current).toBe(undefined);
10+
});
11+
12+
test('can specify initial value', () => {
13+
const subject$ = new Subject();
14+
const { result } = renderHook(() => useObservable(subject$, 123));
15+
16+
expect(result.current).toBe(123);
17+
});
18+
19+
test('returns the latest value of observables', () => {
20+
const subject$ = new Subject();
21+
const { result } = renderHook(() => useObservable(subject$, 123));
22+
23+
act(() => {
24+
subject$.next(125);
25+
});
26+
expect(result.current).toBe(125);
27+
28+
act(() => {
29+
subject$.next(300);
30+
subject$.next(400);
31+
});
32+
expect(result.current).toBe(400);
33+
});
34+
35+
xtest('subscribes to observable only once', () => {});

src/useObservable.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import { useEffect, useState } from 'react';
22

3-
const useObservable = <T>(observable$, initialValue?: T): T | undefined => {
3+
export interface Observable<T> {
4+
subscribe: (
5+
listener: (value: T) => void
6+
) => {
7+
unsubscribe: () => void;
8+
};
9+
}
10+
11+
function useObservable<T>(observable$: Observable<T>): T | undefined;
12+
function useObservable<T>(observable$: Observable<T>, initialValue: T): T;
13+
function useObservable<T>(observable$: Observable<T>, initialValue?: T): T | undefined {
414
const [value, update] = useState<T | undefined>(initialValue);
515

616
useEffect(() => {
@@ -9,6 +19,6 @@ const useObservable = <T>(observable$, initialValue?: T): T | undefined => {
919
}, [observable$]);
1020

1121
return value;
12-
};
22+
}
1323

1424
export default useObservable;

src/useSetState.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { useState } from 'react';
22

3-
const useSetState = <T extends object>(initialState: T = {} as T): [T, (patch: Partial<T> | (() => void)) => void] => {
3+
const useSetState = <T extends object>(
4+
initialState: T = {} as T
5+
): [T, (patch: Partial<T> | ((prevState: T) => Partial<T>)) => void] => {
46
const [state, set] = useState<T>(initialState);
57
const setState = patch => {
68
set(prevState => Object.assign({}, prevState, patch instanceof Function ? patch(prevState) : patch));

yarn.lock

+21-53
Original file line numberDiff line numberDiff line change
@@ -3432,7 +3432,7 @@ babel-plugin-add-react-displayname@^0.0.5:
34323432
resolved "https://registry.yarnpkg.com/babel-plugin-add-react-displayname/-/babel-plugin-add-react-displayname-0.0.5.tgz#339d4cddb7b65fd62d1df9db9fe04de134122bd5"
34333433
integrity sha1-M51M3be2X9YtHfnbn+BN4TQSK9U=
34343434

3435-
3435+
[email protected], babel-plugin-dynamic-import-node@^2.2.0:
34363436
version "2.2.0"
34373437
resolved "https://registry.yarnpkg.com/babel-plugin-dynamic-import-node/-/babel-plugin-dynamic-import-node-2.2.0.tgz#c0adfb07d95f4a4495e9aaac6ec386c4d7c2524e"
34383438
integrity sha512-fP899ELUnTaBcIzmrW7nniyqqdYWrWuJUyPWHxFa/c7r7hS6KC8FscNfLlBNIoPSc55kYMGEEKjPjJGCLbE1qA==
@@ -5246,7 +5246,7 @@ debug@^4.0.1, debug@^4.1.0, debug@^4.1.1:
52465246
dependencies:
52475247
ms "^2.1.1"
52485248

5249-
debuglog@*, debuglog@^1.0.1:
5249+
debuglog@^1.0.1:
52505250
version "1.0.1"
52515251
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
52525252
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
@@ -7264,7 +7264,7 @@ import-local@^2.0.0:
72647264
pkg-dir "^3.0.0"
72657265
resolve-cwd "^2.0.0"
72667266

7267-
imurmurhash@*, imurmurhash@^0.1.4:
7267+
imurmurhash@^0.1.4:
72687268
version "0.1.4"
72697269
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
72707270
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
@@ -8585,7 +8585,7 @@ libnpm@^2.0.1:
85858585
read-package-json "^2.0.13"
85868586
stringify-package "^1.0.0"
85878587

8588-
libnpmaccess@*, libnpmaccess@^3.0.1:
8588+
libnpmaccess@^3.0.1:
85898589
version "3.0.1"
85908590
resolved "https://registry.yarnpkg.com/libnpmaccess/-/libnpmaccess-3.0.1.tgz#5b3a9de621f293d425191aa2e779102f84167fa8"
85918591
integrity sha512-RlZ7PNarCBt+XbnP7R6PoVgOq9t+kou5rvhaInoNibhPO7eMlRfS0B8yjatgn2yaHIwWNyoJDolC/6Lc5L/IQA==
@@ -8622,7 +8622,7 @@ libnpmhook@^5.0.2:
86228622
get-stream "^4.0.0"
86238623
npm-registry-fetch "^3.8.0"
86248624

8625-
libnpmorg@*, libnpmorg@^1.0.0:
8625+
libnpmorg@^1.0.0:
86268626
version "1.0.0"
86278627
resolved "https://registry.yarnpkg.com/libnpmorg/-/libnpmorg-1.0.0.tgz#979b868c48ba28c5820e3bb9d9e73c883c16a232"
86288628
integrity sha512-o+4eVJBoDGMgRwh2lJY0a8pRV2c/tQM/SxlqXezjcAg26Qe9jigYVs+Xk0vvlYDWCDhP0g74J8UwWeAgsB7gGw==
@@ -8647,7 +8647,7 @@ libnpmpublish@^1.1.0:
86478647
semver "^5.5.1"
86488648
ssri "^6.0.1"
86498649

8650-
libnpmsearch@*, libnpmsearch@^2.0.0:
8650+
libnpmsearch@^2.0.0:
86518651
version "2.0.0"
86528652
resolved "https://registry.yarnpkg.com/libnpmsearch/-/libnpmsearch-2.0.0.tgz#de05af47ada81554a5f64276a69599070d4a5685"
86538653
integrity sha512-vd+JWbTGzOSfiOc+72MU6y7WqmBXn49egCCrIXp27iE/88bX8EpG64ST1blWQI1bSMUr9l1AKPMVsqa2tS5KWA==
@@ -8656,7 +8656,7 @@ libnpmsearch@*, libnpmsearch@^2.0.0:
86568656
get-stream "^4.0.0"
86578657
npm-registry-fetch "^3.8.0"
86588658

8659-
libnpmteam@*, libnpmteam@^1.0.1:
8659+
libnpmteam@^1.0.1:
86608660
version "1.0.1"
86618661
resolved "https://registry.yarnpkg.com/libnpmteam/-/libnpmteam-1.0.1.tgz#ff704b1b6c06ea674b3b1101ac3e305f5114f213"
86628662
integrity sha512-gDdrflKFCX7TNwOMX1snWojCoDE5LoRWcfOC0C/fqF7mBq8Uz9zWAX4B2RllYETNO7pBupBaSyBDkTAC15cAMg==
@@ -8837,11 +8837,6 @@ lodash-es@^4.17.11:
88378837
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.11.tgz#145ab4a7ac5c5e52a3531fb4f310255a152b4be0"
88388838
integrity sha512-DHb1ub+rMjjrxqlB3H56/6MXtm1lSksDp2rA2cNWjG8mlDUYFhUj3Di2Zn5IwSU87xLv8tNIQ7sSwE/YOX/D/Q==
88398839

8840-
lodash._baseindexof@*:
8841-
version "3.1.0"
8842-
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
8843-
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=
8844-
88458840
lodash._baseuniq@~4.6.0:
88468841
version "4.6.0"
88478842
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
@@ -8850,33 +8845,11 @@ lodash._baseuniq@~4.6.0:
88508845
lodash._createset "~4.0.0"
88518846
lodash._root "~3.0.0"
88528847

8853-
lodash._bindcallback@*:
8854-
version "3.0.1"
8855-
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
8856-
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=
8857-
8858-
lodash._cacheindexof@*:
8859-
version "3.0.2"
8860-
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
8861-
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=
8862-
8863-
lodash._createcache@*:
8864-
version "3.1.2"
8865-
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
8866-
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
8867-
dependencies:
8868-
lodash._getnative "^3.0.0"
8869-
88708848
lodash._createset@~4.0.0:
88718849
version "4.0.3"
88728850
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
88738851
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=
88748852

8875-
lodash._getnative@*, lodash._getnative@^3.0.0:
8876-
version "3.9.1"
8877-
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
8878-
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=
8879-
88808853
lodash._root@~3.0.0:
88818854
version "3.0.1"
88828855
resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
@@ -8932,11 +8905,6 @@ lodash.pick@^4.4.0:
89328905
resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3"
89338906
integrity sha1-UvBWEP/53tQiYRRB7R/BI6AwAbM=
89348907

8935-
lodash.restparam@*:
8936-
version "3.6.1"
8937-
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
8938-
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=
8939-
89408908
lodash.some@^4.6.0:
89418909
version "4.6.0"
89428910
resolved "https://registry.yarnpkg.com/lodash.some/-/lodash.some-4.6.0.tgz#1bb9f314ef6b8baded13b549169b2a945eb68e4d"
@@ -9955,15 +9923,6 @@ npm-pick-manifest@^2.2.3:
99559923
npm-package-arg "^6.0.0"
99569924
semver "^5.4.1"
99579925

9958-
npm-profile@*, npm-profile@^4.0.1:
9959-
version "4.0.1"
9960-
resolved "https://registry.yarnpkg.com/npm-profile/-/npm-profile-4.0.1.tgz#d350f7a5e6b60691c7168fbb8392c3603583f5aa"
9961-
integrity sha512-NQ1I/1Q7YRtHZXkcuU1/IyHeLy6pd+ScKg4+DQHdfsm769TGq6HPrkbuNJVJS4zwE+0mvvmeULzQdWn2L2EsVA==
9962-
dependencies:
9963-
aproba "^1.1.2 || 2"
9964-
figgy-pudding "^3.4.1"
9965-
npm-registry-fetch "^3.8.0"
9966-
99679926
npm-profile@^3.0.2:
99689927
version "3.0.2"
99699928
resolved "https://registry.yarnpkg.com/npm-profile/-/npm-profile-3.0.2.tgz#58d568f1b56ef769602fd0aed8c43fa0e0de0f57"
@@ -9972,6 +9931,15 @@ npm-profile@^3.0.2:
99729931
aproba "^1.1.2 || 2"
99739932
make-fetch-happen "^2.5.0 || 3 || 4"
99749933

9934+
npm-profile@^4.0.1:
9935+
version "4.0.1"
9936+
resolved "https://registry.yarnpkg.com/npm-profile/-/npm-profile-4.0.1.tgz#d350f7a5e6b60691c7168fbb8392c3603583f5aa"
9937+
integrity sha512-NQ1I/1Q7YRtHZXkcuU1/IyHeLy6pd+ScKg4+DQHdfsm769TGq6HPrkbuNJVJS4zwE+0mvvmeULzQdWn2L2EsVA==
9938+
dependencies:
9939+
aproba "^1.1.2 || 2"
9940+
figgy-pudding "^3.4.1"
9941+
npm-registry-fetch "^3.8.0"
9942+
99759943
npm-registry-client@^8.6.0:
99769944
version "8.6.0"
99779945
resolved "https://registry.yarnpkg.com/npm-registry-client/-/npm-registry-client-8.6.0.tgz#7f1529f91450732e89f8518e0f21459deea3e4c4"
@@ -11834,7 +11802,7 @@ readable-stream@~1.1.10:
1183411802
isarray "0.0.1"
1183511803
string_decoder "~0.10.x"
1183611804

11837-
readdir-scoped-modules@*, readdir-scoped-modules@^1.0.0:
11805+
readdir-scoped-modules@^1.0.0:
1183811806
version "1.0.2"
1183911807
resolved "https://registry.yarnpkg.com/readdir-scoped-modules/-/readdir-scoped-modules-1.0.2.tgz#9fafa37d286be5d92cbaebdee030dc9b5f406747"
1184011808
integrity sha1-n6+jfShr5dksuuve4DDcm19AZ0c=
@@ -13755,10 +13723,10 @@ typedarray@^0.0.6:
1375513723
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
1375613724
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
1375713725

13758-
typescript@3.4.5:
13759-
version "3.4.5"
13760-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.4.5.tgz#2d2618d10bb566572b8d7aad5180d84257d70a99"
13761-
integrity sha512-YycBxUb49UUhdNMU5aJ7z5Ej2XGmaIBL0x34vZ82fn3hGvD+bgrMrVDpatgz2f7YxUMJxMkbWxJZeAvDxVe7Vw==
13726+
typescript@3.5.1:
13727+
version "3.5.1"
13728+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.5.1.tgz#ba72a6a600b2158139c5dd8850f700e231464202"
13729+
integrity sha512-64HkdiRv1yYZsSe4xC1WVgamNigVYjlssIoaH2HcZF0+ijsk5YK2g0G34w9wJkze8+5ow4STd22AynfO6ZYYLw==
1376213730

1376313731
ua-parser-js@^0.7.18:
1376413732
version "0.7.19"

0 commit comments

Comments
 (0)