Skip to content

Commit ead142c

Browse files
committed
feat: 🎸 add useCallbag hook
1 parent 2a43c37 commit ead142c

File tree

8 files changed

+70
-3
lines changed

8 files changed

+70
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<br/>
7878
- [**State**](./docs/State.md)
7979
- [`createMemo`](./docs/createMemo.md) &mdash; factory of memoized hooks.
80+
- [`useCallbag`](./docs/useCallbag.md) &mdash; tracks latest value of a callbag.
8081
- [`useGetSet`](./docs/useGetSet.md) &mdash; returns state getter `get()` instead of raw state.
8182
- [`useGetSetState`](./docs/useGetSetState.md) &mdash; as if [`useGetSet`](./docs/useGetSet.md) and [`useSetState`](./docs/useSetState.md) had a baby.
8283
- [`useObservable`](./docs/useObservable.md) &mdash; tracks latest value of an `Observable`.

docs/useCallbag.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# `useCallbag`
2+
3+
React state hook that tracks the latest value of a callbag.
4+
5+
6+
## Usage
7+
8+
```jsx
9+
import {useCallbag} from 'react-use';
10+
import interval from 'callbag-interval';
11+
12+
const Demo = () => {
13+
const count = useCallbag(() => interval(1000));
14+
return <span>{`Counter: ${count}`}</span>
15+
};
16+
```

docs/useObservable.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ React state hook that tracks the latest value of an `Observable`.
66
## Usage
77

88
```jsx
9+
import {useObservable} from 'react-use';
10+
911
const counter$ = new BehaviorSubject(0);
1012
const Demo = () => {
1113
const value = useObservable(counter$, 0);
12-
14+
1315
return (
1416
<button onClick={() => counter$.next(value + 1)}>
1517
Clicked {value} times

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"nano-css": "^3.4.0",
3737
"rebound": "^0.1.0",
3838
"ts-easing": "^0.1.0",
39-
"use-onclickoutside": "^0.1.0"
39+
"use-onclickoutside": "^0.1.0",
40+
"use-callbag": "^0.1.2"
4041
},
4142
"devDependencies": {
4243
"@storybook/react": "^3.4.11",
@@ -52,7 +53,8 @@
5253
"@semantic-release/changelog": "^3.0.1",
5354
"@semantic-release/npm": "^5.0.5",
5455
"@semantic-release/git": "^7.0.5",
55-
"rxjs": "^6.3.3"
56+
"rxjs": "^6.3.3",
57+
"callbag-interval": "^1.1.0"
5658
},
5759
"peerDependencies": {
5860
"react": "16.7.0-alpha.0",

src/__stories__/useCallbag.story.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {useCallbag} from '..';
2+
import * as React from 'react';
3+
import {storiesOf} from '@storybook/react';
4+
import ShowDocs from '../util/ShowDocs';
5+
const interval = require('callbag-interval').default;
6+
7+
const Demo = () => {
8+
const count = useCallbag(() => (interval as any)(1000));
9+
return <span>{`Counter: ${count}`}</span>
10+
};
11+
12+
storiesOf('useCallbag', module)
13+
.add('Docs', () => <ShowDocs md={require('../../docs/useCallbag.md')} />)
14+
.add('Demo', () =>
15+
<Demo/>
16+
)

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import useAsync from './useAsync';
33
import useAudio from './useAudio';
44
import useBattery from './useBattery';
55
import useBoolean from './useBoolean';
6+
import useCallbag from './useCallbag';
67
import useCounter from './useCounter';
78
import useCss from './useCss';
89
import useFavicon from './useFavicon';
@@ -46,6 +47,7 @@ export {
4647
useAudio,
4748
useBattery,
4849
useBoolean,
50+
useCallbag,
4951
useCounter,
5052
useCss,
5153
useFavicon,

src/useCallbag.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import useCallbag from 'use-callbag'
2+
3+
export default useCallbag;
4+

yarn.lock

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2156,6 +2156,23 @@ call-me-maybe@^1.0.1:
21562156
resolved "https://registry.yarnpkg.com/call-me-maybe/-/call-me-maybe-1.0.1.tgz#26d208ea89e37b5cbde60250a15f031c16a4d66b"
21572157
integrity sha1-JtII6onje1y95gJQoV8DHBak1ms=
21582158

2159+
callbag-interval@^1.1.0:
2160+
version "1.1.0"
2161+
resolved "https://registry.yarnpkg.com/callbag-interval/-/callbag-interval-1.1.0.tgz#161ae4b5bf36b98bd5ef7caa3c3f8161fc032338"
2162+
integrity sha512-xkYj7t/l9+GaP0kvPi6LadF3ZNSiQAhbEzwnnTrDB/UtkBCrzKPZ9Kiti8P8oHYxL2jGEuRWi86PD3uM7qXLbg==
2163+
dependencies:
2164+
callbag "^1.1.0"
2165+
2166+
callbag-subscribe@^1.4.1:
2167+
version "1.4.1"
2168+
resolved "https://registry.yarnpkg.com/callbag-subscribe/-/callbag-subscribe-1.4.1.tgz#f9bd86b713230c9113acf6b7c5c6baf7d5ec772f"
2169+
integrity sha512-FzvJK3ODP50UhZPhfCFwJZ+YP+Hg/6Fm1+d3m2UZacA1b+jSKARpyNTIpkWlf61rAHEgpwZF1head4URY+EEMw==
2170+
2171+
callbag@^1.1.0:
2172+
version "1.1.0"
2173+
resolved "https://registry.yarnpkg.com/callbag/-/callbag-1.1.0.tgz#9cbbc3352832da43e764d588ed43121d4b09c42a"
2174+
integrity sha512-JVZp2vA53+THwOOan8eedGhS7xwzwR1u8HFK02WDMI9pITGoQ42j2sYP742qs7yddtEjIDGgNe1Y6DN90w3oIQ==
2175+
21592176
camel-case@3.0.x:
21602177
version "3.0.0"
21612178
resolved "https://registry.yarnpkg.com/camel-case/-/camel-case-3.0.0.tgz#ca3c3688a4e9cf3a4cda777dc4dcbc713249cf73"
@@ -9512,6 +9529,13 @@ url@^0.11.0:
95129529
punycode "1.3.2"
95139530
querystring "0.2.0"
95149531

9532+
use-callbag@^0.1.2:
9533+
version "0.1.2"
9534+
resolved "https://registry.yarnpkg.com/use-callbag/-/use-callbag-0.1.2.tgz#8f214952ccc488159b185ffe7ff17fccba7a73d3"
9535+
integrity sha512-8JJIpoqQaqJu9nfSTYe4WpJm2NyfHYgIDjouxhu4WOpgYI8W9IvMM32ecfcdZTzkWDuZkEyh3unGLMxA4wF7Bg==
9536+
dependencies:
9537+
callbag-subscribe "^1.4.1"
9538+
95159539
use-onclickoutside@^0.1.0:
95169540
version "0.1.0"
95179541
resolved "https://registry.yarnpkg.com/use-onclickoutside/-/use-onclickoutside-0.1.0.tgz#bc1998fe36728b22a4b9b28c95fbb468b3033e84"

0 commit comments

Comments
 (0)