All Projects → Sanjagh → use-custom-compare-effect

Sanjagh / use-custom-compare-effect

Licence: MIT License
useEffect hook which takes a comparison function instead of compare using reference equality

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to use-custom-compare-effect

react-hooks-example
React Hooks Example
Stars: ✭ 21 (-19.23%)
Mutual labels:  react-hooks
react-social-network
react social network
Stars: ✭ 13 (-50%)
Mutual labels:  react-hooks
rxdb-hooks
React hooks for integrating with RxDB
Stars: ✭ 57 (+119.23%)
Mutual labels:  react-hooks
Fakeflix
Not the usual clone that you can find on the web.
Stars: ✭ 4,429 (+16934.62%)
Mutual labels:  react-hooks
react-emotion-multi-step-form
React multi-step form library with Emotion styling
Stars: ✭ 25 (-3.85%)
Mutual labels:  react-hooks
react-admin-nest
React和Ant Design和 Nest.js 和 Mysql 构建的后台通用管理系统。持续更新。
Stars: ✭ 123 (+373.08%)
Mutual labels:  react-hooks
react-sendbird-messenger
ReactJS (React-router-dom v6 + Antdesign + Firebase + Sendbird + Sentry) codebase containing real world examples (CRUD, auth, advanced patterns, etc).
Stars: ✭ 39 (+50%)
Mutual labels:  react-hooks
react-captain
⚓ A collection of strongly typed React hooks and contexts.
Stars: ✭ 15 (-42.31%)
Mutual labels:  react-hooks
MetFlix
A Movie app demo. Like NetFlix ❤️
Stars: ✭ 50 (+92.31%)
Mutual labels:  react-hooks
react-hook-form-auto
Automatic form generation using ReactHookForm
Stars: ✭ 45 (+73.08%)
Mutual labels:  react-hooks
window-scroll-position
React hook for Window scroll position
Stars: ✭ 81 (+211.54%)
Mutual labels:  react-hooks
Facebook-Messenger
This is a Facebook Messenger clone.You can comminicate to other at realtime.Used ReactJS, Material UI, Firebase, Firestore Database
Stars: ✭ 18 (-30.77%)
Mutual labels:  react-hooks
frontegg-react
Frontegg-React is pre-built Component for faster and simpler integration with Frontegg services.
Stars: ✭ 15 (-42.31%)
Mutual labels:  react-hooks
learn-react-typescript
Learning React contents with TypeScript (Hooks, Redux)
Stars: ✭ 15 (-42.31%)
Mutual labels:  react-hooks
react-store
A library for better state management in react hooks world
Stars: ✭ 34 (+30.77%)
Mutual labels:  react-hooks
react-hooks-file-upload
React Hooks File Upload example with Progress Bar using Axios, Bootstrap
Stars: ✭ 30 (+15.38%)
Mutual labels:  react-hooks
atomic-state
A decentralized state management library for React
Stars: ✭ 54 (+107.69%)
Mutual labels:  react-hooks
MyApp
A template to create awesome Apps easily ⚡️
Stars: ✭ 39 (+50%)
Mutual labels:  react-hooks
use-url-search-params
A React Hook to use URL query string as a state management
Stars: ✭ 51 (+96.15%)
Mutual labels:  react-hooks
how-react-hooks-work
Understand how React-hook really behaves, once and for all!
Stars: ✭ 73 (+180.77%)
Mutual labels:  react-hooks

use-custom-compare-effect

useEffect hook which takes a comparison function instead of compare using reference equality (inspired by use-deep-compare-effect)

Why?

You can solve the problem of using objects as useEffect via deep compare effect explained in the link. but the key here is that deep comparisons are slow and have unpredictable performance. The idea behind useCustomCompareEffect is you probably know the shape of the data you're passing as effect dependency so that you can write a cheap and predictable compare function to decide whether run the effect or not. another use case would be running effect only when certain properties changed.

Note: This works best when you're generating code (let's say client side api for your backend) cause you know the exact shape of the data so you can also generate cheap compare functions as an alternative to deep comparisons or JSON.stringify, That's what we are doing at Sanjagh.

Install

npm install use-custom-compare-effect
# or yarn
yarn add use-custom-compare-effect

How to use

You can provide your custom compare function as the third params like below:

import useCustomCompareEffect from 'use-custom-compare-effect';

// ... usage
useCustomCompareEffect(() => {
  // ... your usual callback here
  // do things like fetch network request or the like
  // the second params dont have to be an array, it can be object, number, etc
}, {a: 'things'}, (a, b) => {
  // and here is your custom compare function
  // you can check difference between old(a variable) and new(b variable)
  // in here like so
  return a.a === b.a;
})

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].