Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: streamich/react-use
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v12.2.3
Choose a base ref
...
head repository: streamich/react-use
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v12.3.0
Choose a head ref
  • 11 commits
  • 15 files changed
  • 5 contributors

Commits on Oct 5, 2019

  1. Copy the full SHA
    a6afe92 View commit details

Commits on Oct 6, 2019

  1. Copy the full SHA
    ae65105 View commit details
  2. docs: useStateList demo link (#647)

    artywhite authored and wardoost committed Oct 6, 2019
    Copy the full SHA
    529362e View commit details
  3. docs: usePrevious demo link (#651)

    artywhite authored and wardoost committed Oct 6, 2019
    Copy the full SHA
    9f003fd View commit details

Commits on Oct 7, 2019

  1. docs: useToggle demo link (#648)

    artywhite authored and wardoost committed Oct 7, 2019

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    b1fc6a7 View commit details
  2. docs: useMap demo link + improved docs (#650)

    * improve docs: useMap demo link
    
    * improve docs: useMap demo link (remove callback usage added)
    
    * Add remove action to useMap story
    artywhite authored and wardoost committed Oct 7, 2019
    Copy the full SHA
    c520797 View commit details
  3. docs: useList demo link + improve docs (#649)

    * improve docs: useList demo link
    
    * improve docs: useList demo link (examples of callbacks usage)
    
    * Add all callback methods to useList story
    artywhite authored and wardoost committed Oct 7, 2019
    Copy the full SHA
    4f48494 View commit details
  4. Copy the full SHA
    03ccfa9 View commit details
  5. Copy the full SHA
    471da2a View commit details
  6. Copy the full SHA
    9ea3548 View commit details
  7. chore(release): 12.3.0 [skip ci]

    # [12.3.0](v12.2.3...v12.3.0) (2019-10-07)
    
    ### Features
    
    * reset util callback for useList ([#654](#654)) ([9ea3548](9ea3548))
    semantic-release-bot committed Oct 7, 2019
    Copy the full SHA
    2ab1460 View commit details
Showing with 131 additions and 41 deletions.
  1. +7 βˆ’0 CHANGELOG.md
  2. +5 βˆ’5 README.md
  3. +15 βˆ’5 docs/useList.md
  4. +10 βˆ’4 docs/useMap.md
  5. +5 βˆ’1 docs/usePrevious.md
  6. +4 βˆ’1 docs/useSearchParam.md
  7. +1 βˆ’1 docs/useToggle.md
  8. +6 βˆ’1 docs/useUpsert.md
  9. +3 βˆ’3 package.json
  10. +11 βˆ’4 src/__stories__/useList.story.tsx
  11. +5 βˆ’2 src/__stories__/useMap.story.tsx
  12. +41 βˆ’0 src/__tests__/useList.test.ts
  13. +3 βˆ’3 src/useFullscreen.ts
  14. +10 βˆ’6 src/useList.ts
  15. +5 βˆ’5 yarn.lock
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# [12.3.0](https://github.com/streamich/react-use/compare/v12.2.3...v12.3.0) (2019-10-07)


### Features

* reset util callback for useList ([#654](https://github.com/streamich/react-use/issues/654)) ([9ea3548](https://github.com/streamich/react-use/commit/9ea3548))

## [12.2.3](https://github.com/streamich/react-use/compare/v12.2.2...v12.2.3) (2019-10-05)


10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -125,14 +125,14 @@
- [`useDefault`](./docs/useDefault.md) — returns the default value when state is `null` or `undefined`.
- [`useGetSet`](./docs/useGetSet.md) — returns state getter `get()` instead of raw state.
- [`useGetSetState`](./docs/useGetSetState.md) — as if [`useGetSet`](./docs/useGetSet.md) and [`useSetState`](./docs/useSetState.md) had a baby.
- [`usePrevious`](./docs/usePrevious.md) — returns the previous state or props.
- [`usePrevious`](./docs/usePrevious.md) — returns the previous state or props. [![][img-demo]](https://codesandbox.io/s/fervent-galileo-krgx6)
- [`useObservable`](./docs/useObservable.md) — tracks latest value of an `Observable`.
- [`useSetState`](./docs/useSetState.md) — creates `setState` method which works like `this.setState`. [![][img-demo]](https://codesandbox.io/s/n75zqn1xp0)
- [`useStateList`](./docs/useStateList.md) — circularly iterates over an array.
- [`useToggle` and `useBoolean`](./docs/useToggle.md) — tracks state of a boolean.
- [`useStateList`](./docs/useStateList.md) — circularly iterates over an array. [![][img-demo]](https://codesandbox.io/s/bold-dewdney-pjzkd)
- [`useToggle` and `useBoolean`](./docs/useToggle.md) — tracks state of a boolean. [![][img-demo]](https://codesandbox.io/s/focused-sammet-brw2d)
- [`useCounter` and `useNumber`](./docs/useCounter.md) — tracks state of a number. [![][img-demo]](https://streamich.github.io/react-use/?path=/story/state-usecounter--demo)
- [`useList`](./docs/useList.md) — tracks state of an array.
- [`useMap`](./docs/useMap.md) — tracks state of an object.
- [`useList`](./docs/useList.md) and [`useUpsert`](./docs/useUpsert.md) — tracks state of an array. [![][img-demo]](https://codesandbox.io/s/wonderful-mahavira-1sm0w)
- [`useMap`](./docs/useMap.md) — tracks state of an object. [![][img-demo]](https://codesandbox.io/s/quirky-dewdney-gi161)


<br />
20 changes: 15 additions & 5 deletions docs/useList.md
Original file line number Diff line number Diff line change
@@ -2,21 +2,31 @@

React state hook that tracks a value of an array.


## Usage

```jsx
import {useList} from 'react-use';

const Demo = () => {
const [list, {set, push}] = useList();
const [list, { clear, filter, push, remove, set, sort, updateAt, reset }] = useList();

return (
<div>
<div>{list.join(',')}</div>
<button onClick={() => set([])}>Reset</button>
<button onClick={() => push(Date.now())}>Push</button>
<button onClick={() => set([1, 2, 3])}>Set to [1, 2, 3]</button>
<button onClick={() => push(Date.now())}>Push timestamp</button>
<button onClick={() => updateAt(1, Date.now())}>Update value at index 1</button>
<button onClick={() => remove(1)}>Remove element at index 1</button>
<button onClick={() => filter(item => item % 2 === 0)}>Filter even values</button>
<button onClick={() => sort((a, b) => a - b)}>Sort ascending</button>
<button onClick={() => sort((a, b) => b - a)}>Sort descending</button>
<button onClick={clear}>Clear</button>
<button onClick={reset}>Reset</button>
<pre>{JSON.stringify(list, null, 2)}</pre>
</div>
);
};
```

## Related hooks

- [useUpsert](./useUpsert.md)
14 changes: 10 additions & 4 deletions docs/useMap.md
Original file line number Diff line number Diff line change
@@ -2,22 +2,28 @@

React state hook that tracks a value of an object.


## Usage

```jsx
import {useMap} from 'react-use';

const Demo = () => {
const [map, {set, reset}] = useMap({
const [map, {set, remove, reset}] = useMap({
hello: 'there',
});

return (
<div>
<button onClick={() => set(String(Date.now()), new Date().toJSON())}>
Add
</button>
<button onClick={() => reset()}>
Reset
</button>
<button onClick={() => remove('hello')} disabled={!map.hello}>
Remove 'hello'
</button>
<pre>{JSON.stringify(map, null, 2)}</pre>
<button onClick={() => set(String(Date.now()), (new Date()).toJSON())}>Add</button>
<button onClick={() => reset()}>Reset</button>
</div>
);
};
6 changes: 5 additions & 1 deletion docs/usePrevious.md
Original file line number Diff line number Diff line change
@@ -13,7 +13,11 @@ const Demo = () => {

return (
<p>
Now: {count}, before: {prevCount}
<button onClick={() => setCount(count + 1)}>+</button>
<button onClick={() => setCount(count - 1)}>-</button>
<p>
Now: {count}, before: {prevCount}
</p>
</p>
);
};
5 changes: 4 additions & 1 deletion docs/useSearchParam.md
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@

React sensor hook that tracks browser's location search param.


## Usage

```jsx
@@ -27,3 +26,7 @@ const Demo = () => {
);
};
```
## Caveats/Gotchas
When using a hash router, like `react-router`'s [`<HashRouter>`](https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/docs/api/HashRouter.md), this hook won't be able to read the search parameters as they are considered part of the hash of the URL by browsers.
2 changes: 1 addition & 1 deletion docs/useToggle.md
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ React state hook that tracks value of a boolean.
## Usage

```jsx
import {useToggle, useBoolean} from 'react-use';
import {useToggle} from 'react-use';

const Demo = () => {
const [on, toggle] = useToggle(true);
7 changes: 6 additions & 1 deletion docs/useUpsert.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# `useUpsert`

Superset of `useList`. Provides an additional method to upsert (update or insert) an element into the list.
Superset of [`useList`](./useList.md). Provides an additional method to upsert (update or insert) an element into the list.

## Usage

```jsx
@@ -26,3 +27,7 @@ const Demo = () => {
);
};
```

## Related hooks

- [useList](./useList.md)
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-use",
"version": "12.2.3",
"version": "12.3.0",
"description": "Collection of React Hooks",
"main": "lib/index.js",
"module": "esm/index.js",
@@ -50,7 +50,7 @@
"react-fast-compare": "^2.0.4",
"react-wait": "^0.3.0",
"resize-observer-polyfill": "^1.5.1",
"screenfull": "^4.1.0",
"screenfull": "^5.0.0",
"set-harmonic-interval": "^1.0.0",
"throttle-debounce": "^2.0.1",
"ts-easing": "^0.2.0"
@@ -76,7 +76,7 @@
"@testing-library/react-hooks": "2.0.3",
"@types/jest": "24.0.18",
"@types/react": "16.9.2",
"@types/react-wait": "^0.3.0",
"@types/react-wait": "0.3.0",
"babel-core": "6.26.3",
"babel-loader": "8.0.6",
"babel-plugin-dynamic-import-node": "2.3.0",
15 changes: 11 additions & 4 deletions src/__stories__/useList.story.tsx
Original file line number Diff line number Diff line change
@@ -4,13 +4,20 @@ import { useList } from '..';
import ShowDocs from './util/ShowDocs';

const Demo = () => {
const [list, { set, push }] = useList();
const [list, { clear, filter, push, remove, set, sort, updateAt, reset }] = useList([1, 2, 3, 4, 5]);

return (
<div>
<div>{list.join(',')}</div>
<button onClick={() => set([])}>Reset</button>
<button onClick={() => push(Date.now())}>Push</button>
<button onClick={() => set([1, 2, 3])}>Set to [1, 2, 3]</button>
<button onClick={() => push(Date.now())}>Push timestamp</button>
<button onClick={() => updateAt(1, Date.now())}>Update value at index 1</button>
<button onClick={() => remove(1)}>Remove element at index 1</button>
<button onClick={() => filter(item => item % 2 === 0)}>Filter even values</button>
<button onClick={() => sort((a, b) => a - b)}>Sort ascending</button>
<button onClick={() => sort((a, b) => b - a)}>Sort descending</button>
<button onClick={clear}>Clear</button>
<button onClick={reset}>Reset</button>
<pre>{JSON.stringify(list, null, 2)}</pre>
</div>
);
};
7 changes: 5 additions & 2 deletions src/__stories__/useMap.story.tsx
Original file line number Diff line number Diff line change
@@ -4,15 +4,18 @@ import { useMap } from '..';
import ShowDocs from './util/ShowDocs';

const Demo = () => {
const [map, { set, reset }] = useMap({
const [map, { set, remove, reset }] = useMap({
hello: 'there',
});

return (
<div>
<pre>{JSON.stringify(map, null, 2)}</pre>
<button onClick={() => set(String(Date.now()), new Date().toJSON())}>Add</button>
<button onClick={() => reset()}>Reset</button>
<button onClick={() => remove('hello')} disabled={!map.hello}>
Remove 'hello'
</button>
<pre>{JSON.stringify(map, null, 2)}</pre>
</div>
);
};
41 changes: 41 additions & 0 deletions src/__tests__/useList.test.ts
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ it('should init list and utils', () => {
push: expect.any(Function),
filter: expect.any(Function),
sort: expect.any(Function),
reset: expect.any(Function),
});
});

@@ -150,3 +151,43 @@ it('should sort current list by provided function', () => {
expect(result.current[0]).toEqual(['March', 'Jan', 'Feb', 'Dec']);
expect(result.current[0]).not.toBe(initList); // checking immutability
});

it('should reset the list to initial list provided', () => {
const initList = [1, 2, 3];
const { result } = setUp(initList);
const [, utils] = result.current;

act(() => {
utils.push(4);
});

expect(result.current[0]).toEqual([1, 2, 3, 4]);

act(() => {
utils.reset();
});

expect(result.current[0]).toEqual([1, 2, 3]);
expect(result.current[0]).not.toBe(initList); // checking immutability
});

it('should memoized its utils methods', () => {
const initList = [1, 2, 3];
const { result } = setUp(initList);
const [, utils] = result.current;
const { set, clear, updateAt, remove, push, filter, sort, reset } = utils;

act(() => {
push(4);
});

expect(result.current[1]).toBe(utils);
expect(result.current[1].set).toBe(set);
expect(result.current[1].clear).toBe(clear);
expect(result.current[1].updateAt).toBe(updateAt);
expect(result.current[1].remove).toBe(remove);
expect(result.current[1].push).toBe(push);
expect(result.current[1].filter).toBe(filter);
expect(result.current[1].sort).toBe(sort);
expect(result.current[1].reset).toBe(reset);
});
6 changes: 3 additions & 3 deletions src/useFullscreen.ts
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ const useFullscreen = (ref: RefObject<Element>, on: boolean, options: FullScreen
};

const onChange = () => {
if (screenfull) {
if (screenfull.isEnabled) {
const isScreenfullFullscreen = screenfull.isFullscreen;
setIsFullscreen(isScreenfullFullscreen);
if (!isScreenfullFullscreen) {
@@ -35,7 +35,7 @@ const useFullscreen = (ref: RefObject<Element>, on: boolean, options: FullScreen
}
};

if (screenfull && screenfull.enabled) {
if (screenfull.isEnabled) {
try {
screenfull.request(ref.current);
setIsFullscreen(true);
@@ -55,7 +55,7 @@ const useFullscreen = (ref: RefObject<Element>, on: boolean, options: FullScreen

return () => {
setIsFullscreen(false);
if (screenfull && screenfull.enabled) {
if (screenfull.isEnabled) {
try {
screenfull.off('change', onChange);
screenfull.exit();
16 changes: 10 additions & 6 deletions src/useList.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useState, useMemo } from 'react';

export interface Actions<T> {
set: (list: T[]) => void;
@@ -8,14 +8,14 @@ export interface Actions<T> {
push: (item: T) => void;
filter: (fn: (value: T) => boolean) => void;
sort: (fn?: (a: T, b: T) => number) => void;
reset: () => void;
}

const useList = <T>(initialList: T[] = []): [T[], Actions<T>] => {
const [list, set] = useState<T[]>(initialList);

return [
list,
{
const utils = useMemo<Actions<T>>(
() => ({
set,
clear: () => set([]),
updateAt: (index, entry) =>
@@ -24,8 +24,12 @@ const useList = <T>(initialList: T[] = []): [T[], Actions<T>] => {
push: entry => set(currentList => [...currentList, entry]),
filter: fn => set(currentList => currentList.filter(fn)),
sort: (fn?) => set(currentList => [...currentList].sort(fn)),
},
];
reset: () => set([...initialList]),
}),
[set]
);

return [list, utils];
};

export default useList;
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -2227,7 +2227,7 @@
dependencies:
"@types/react" "*"

"@types/react-wait@^0.3.0":
"@types/react-wait@0.3.0":
version "0.3.0"
resolved "https://registry.yarnpkg.com/@types/react-wait/-/react-wait-0.3.0.tgz#6f7ef17571a17e72c7864ede8cf7d3aa525a005e"
integrity sha512-5jIfDcHRjqeE7QfZG7kCqOpfrPSvOM1E3/nlKuJ/NZrG/WrhLo/AFr0i72jhTWzyNRo4ex0pshBaiCHksZXH3A==
@@ -11239,10 +11239,10 @@ schema-utils@^1.0.0:
ajv-errors "^1.0.0"
ajv-keywords "^3.1.0"

screenfull@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/screenfull/-/screenfull-4.1.0.tgz#30eb338f615941f5a2cdd96c14e36063d2d9d764"
integrity sha512-/qH0HAmc+ilbZ9Vf8J7RHjjecSdqmjIh98iMkA6uCSKcHdJK1TiXhTbR+cin8rG70xi4Peyz7wW1KJVP6sp30g==
screenfull@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/screenfull/-/screenfull-5.0.0.tgz#5c2010c0e84fd4157bf852877698f90b8cbe96f6"
integrity sha512-yShzhaIoE9OtOhWVyBBffA6V98CDCoyHTsp8228blmqYy1Z5bddzE/4FPiJKlr8DVR4VBiiUyfPzIQPIYDkeMA==

scrollbarwidth@^0.1.3:
version "0.1.3"