https://github.com/karmaniverous/block-refresh
Use as the equalityFn argument to Redux useSelector() when the selected value is an Object.
https://github.com/karmaniverous/block-refresh
react redux useselector
Last synced: 9 months ago
JSON representation
Use as the equalityFn argument to Redux useSelector() when the selected value is an Object.
- Host: GitHub
- URL: https://github.com/karmaniverous/block-refresh
- Owner: karmaniverous
- Created: 2022-07-14T07:53:05.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-08-12T05:56:58.000Z (over 1 year ago)
- Last Synced: 2025-04-05T00:09:46.287Z (9 months ago)
- Topics: react, redux, useselector
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/@karmaniverous/block-refresh
- Size: 2.47 MB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# block-refresh
Use as the equalityFn argument to Redux
[useSelector](https://react-redux.js.org/api/hooks#useselector) when the
selected value is an Object.
To install:
```bash
npm install @karmaniverous/block-refresh
```
To import:
```js
import { blockRefresh } from '@karmaniverous/block-refresh`;
```
See
[tests](https://github.com/karmaniverous/block-refresh/blob/main/lib/blockRefresh/blockRefresh.test.js)
for examples of usage!
# API Documentation
## Functions
-
blockRefresh([a], [b], [options]) ⇒boolean -
Use as the equalityFn argument to Redux useSelector() when the selected
value is an Object. https://react-redux.js.org/api/hooks#useselector
## Typedefs
-
EqalityFn ⇒boolean -
Return true if the two inputs should be considered equal.
## blockRefresh([a], [b], [options]) ⇒ boolean
Use as the equalityFn argument to Redux useSelector() when the selected
value is an Object. https://react-redux.js.org/api/hooks#useselector
**Kind**: global function
**Returns**: boolean - If true, component will not refresh.
| Param | Type | Description |
| --- | --- | --- |
| [a] | any | Current useSelector result. |
| [b] | any | Last useSelector result. |
| [options] | Object | Options object. |
| [options.log] | string | If defined, function will console log a & b values with log value as label when refresh is blocked. |
| [options.path] | string | A Lodash-style path into the selected object. If defined, the equality test will be restricted to that path. Useful when an object has an update timestamp. |
| [options.predicate] | [EqalityFn](#EqalityFn) | Returns true if refresh should be blocked. If predicate is populated, path & refreshUndefined are ignored. |
| [options.refreshUndefined] | boolean | If true, component will refresh when both comparison values are undefined. |
**Example**
```js
// Passed as a function, it will block component refresh if the current &
// previous values are defined & pass _.isEqual.
const obj = useSelector((state) => state.slice.obj, blockRefresh);
```
**Example**
```js
// Deploy as an anonymous function to set options.
const obj = useSelector(
(state) => state.slice.obj,
(a, b) => blockRefresh(a, b, options)
);
```
## EqalityFn ⇒ boolean
Return true if the two inputs should be considered equal.
**Kind**: global typedef
| Param | Type |
| --- | --- |
| [a] | any |
| [b] | any |
---
See more great templates and other tools on
[my GitHub Profile](https://github.com/karmaniverous)!