1 | <div align="center">
|
2 | <h1>use-deep-compare-effect π</h1>
|
3 |
|
4 | <p>It's React's useEffect hook, except using deep comparison on the inputs, not
|
5 | reference equality</p>
|
6 | </div>
|
7 |
|
8 | ---
|
9 |
|
10 |
|
11 | [![Build Status][build-badge]][build]
|
12 | [![Code Coverage][coverage-badge]][coverage]
|
13 | [![version][version-badge]][package]
|
14 | [![downloads][downloads-badge]][npmtrends]
|
15 | [![MIT License][license-badge]][license]
|
16 | [![All Contributors][all-contributors-badge]](#contributors-)
|
17 | [![PRs Welcome][prs-badge]][prs]
|
18 | [![Code of Conduct][coc-badge]][coc]
|
19 |
|
20 |
|
21 | > WARNING: Please _only_ use this if you really can't find a way to use
|
22 | > `React.useEffect`. There's often a better way to do what you're trying to do
|
23 | > than a deep comparison.
|
24 |
|
25 | ## The Problem
|
26 |
|
27 | React's built-in [`useEffect`][react-hooks] hook has a second argument called
|
28 | the "dependencies array" and it allows you to optimize when React will call your
|
29 | effect callback. React will do a comparison between each of the values (via
|
30 | [`Object.is`][object-is]) to determine whether your effect callback should be
|
31 | called.
|
32 |
|
33 | The problem is that if you need to provide an object for one of those
|
34 | dependencies and that object is new every render, then even if none of the
|
35 | properties changed, your effect will get called anyway.
|
36 |
|
37 | ## Table of Contents
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | - [Installation](#installation)
|
43 | - [Usage](#usage)
|
44 | - [Other Solutions](#other-solutions)
|
45 | - [Issues](#issues)
|
46 | - [π Bugs](#-bugs)
|
47 | - [π‘ Feature Requests](#-feature-requests)
|
48 | - [Contributors β¨](#contributors-)
|
49 | - [LICENSE](#license)
|
50 |
|
51 |
|
52 |
|
53 | ## Installation
|
54 |
|
55 | This module is distributed via [npm][npm] which is bundled with [node][node] and
|
56 | should be installed as one of your project's dependencies:
|
57 |
|
58 | ```shell
|
59 | npm install --save use-deep-compare-effect
|
60 | ```
|
61 |
|
62 | ## Usage
|
63 |
|
64 | You use it in place of `React.useEffect`.
|
65 |
|
66 | > NOTE: Only use this if your values are objects or arrays that contain objects.
|
67 | > Otherwise, you should just use `React.useEffect`. In case of "polymorphic"
|
68 | > values (eg: sometimes object, sometimes a boolean), use
|
69 | > `useDeepCompareEffectNoCheck`, but do it at your own risk, as maybe there can
|
70 | > be better approaches to the problem.
|
71 |
|
72 | > NOTE: Be careful when your dependency is an object which contains function.
|
73 | > If that function is defined on the object during a render, then it's changed
|
74 | > and the effect callback will be called every render.
|
75 | > [Issue](https://github.com/kentcdodds/use-deep-compare-effect/issues/47) has more context.
|
76 |
|
77 | Example:
|
78 |
|
79 | ```jsx
|
80 | import React from 'react'
|
81 | import ReactDOM from 'react-dom'
|
82 | import useDeepCompareEffect from 'use-deep-compare-effect'
|
83 |
|
84 | function Query({query, variables}) {
|
85 | // some code...
|
86 |
|
87 | useDeepCompareEffect(
|
88 | () => {
|
89 | // make an HTTP request or whatever with the query and variables
|
90 | // optionally return a cleanup function if necessary
|
91 | },
|
92 | // query is a string, but variables is an object. With the way Query is used
|
93 | // in the example above, `variables` will be a new object every render.
|
94 | // useDeepCompareEffect will do a deep comparison and your callback is only
|
95 | // run when the variables object actually has changes.
|
96 | [query, variables],
|
97 | )
|
98 |
|
99 | return <div>{/* awesome UI here */}</div>
|
100 | }
|
101 | ```
|
102 |
|
103 | ## Other Solutions
|
104 |
|
105 | [use-custom-compare-effect](https://github.com/sanjagh/use-custom-compare-effect)
|
106 |
|
107 | ## Issues
|
108 |
|
109 | _Looking to contribute? Look for the [Good First Issue][good-first-issue]
|
110 | label._
|
111 |
|
112 | ### π Bugs
|
113 |
|
114 | Please file an issue for bugs, missing documentation, or unexpected behavior.
|
115 |
|
116 | [**See Bugs**][bugs]
|
117 |
|
118 | ### π‘ Feature Requests
|
119 |
|
120 | Please file an issue to suggest new features. Vote on feature requests by adding
|
121 | a π. This helps maintainers prioritize what to work on.
|
122 |
|
123 | [**See Feature Requests**][requests]
|
124 |
|
125 | ## Contributors β¨
|
126 |
|
127 | Thanks goes to these people ([emoji key][emojis]):
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | <table>
|
133 | <tr>
|
134 | <td align="center"><a href="https://kentcdodds.com"><img src="https://avatars.githubusercontent.com/u/1500684?v=3?s=100" width="100px;" alt=""/><br /><sub><b>Kent C. Dodds</b></sub></a><br /><a href="https://github.com/kentcdodds/use-deep-compare-effect/commits?author=kentcdodds" title="Code">π»</a> <a href="https://github.com/kentcdodds/use-deep-compare-effect/commits?author=kentcdodds" title="Documentation">π</a> <a href="#infra-kentcdodds" title="Infrastructure (Hosting, Build-Tools, etc)">π</a> <a href="https://github.com/kentcdodds/use-deep-compare-effect/commits?author=kentcdodds" title="Tests">β οΈ</a></td>
|
135 | <td align="center"><a href="https://linkedin.com/in/edygar/en"><img src="https://avatars2.githubusercontent.com/u/566280?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Edygar de Lima Oliveira</b></sub></a><br /><a href="https://github.com/kentcdodds/use-deep-compare-effect/commits?author=edygar" title="Code">π»</a> <a href="https://github.com/kentcdodds/use-deep-compare-effect/commits?author=edygar" title="Tests">β οΈ</a></td>
|
136 | <td align="center"><a href="https://stackshare.io/jdorfman/decisions"><img src="https://avatars1.githubusercontent.com/u/398230?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Justin Dorfman</b></sub></a><br /><a href="#fundingFinding-jdorfman" title="Funding Finding">π</a></td>
|
137 | <td align="center"><a href="https://antonhalim.com"><img src="https://avatars1.githubusercontent.com/u/10498035?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Anton Halim</b></sub></a><br /><a href="https://github.com/kentcdodds/use-deep-compare-effect/commits?author=antonhalim" title="Documentation">π</a></td>
|
138 | <td align="center"><a href="https://michaeldeboey.be"><img src="https://avatars3.githubusercontent.com/u/6643991?v=4?s=100" width="100px;" alt=""/><br /><sub><b>MichaΓ«l De Boey</b></sub></a><br /><a href="https://github.com/kentcdodds/use-deep-compare-effect/commits?author=MichaelDeBoey" title="Code">π»</a></td>
|
139 | <td align="center"><a href="http://linkedin.com/in/tbueschel"><img src="https://avatars3.githubusercontent.com/u/13087421?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Tobias BΓΌschel</b></sub></a><br /><a href="https://github.com/kentcdodds/use-deep-compare-effect/commits?author=tobiasbueschel" title="Documentation">π</a></td>
|
140 | <td align="center"><a href="http://peter.hozak.info/"><img src="https://avatars0.githubusercontent.com/u/1087670?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Peter HozΓ‘k</b></sub></a><br /><a href="https://github.com/kentcdodds/use-deep-compare-effect/pulls?q=is%3Apr+reviewed-by%3AAprillion" title="Reviewed Pull Requests">π</a></td>
|
141 | </tr>
|
142 | <tr>
|
143 | <td align="center"><a href="https://ricardobusquet.com"><img src="https://avatars1.githubusercontent.com/u/7198302?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ricardo Busquet</b></sub></a><br /><a href="https://github.com/kentcdodds/use-deep-compare-effect/pulls?q=is%3Apr+reviewed-by%3Arbusquet" title="Reviewed Pull Requests">π</a></td>
|
144 | <td align="center"><a href="https://github.com/daveisfera"><img src="https://avatars3.githubusercontent.com/u/1686193?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Dave Johansen</b></sub></a><br /><a href="https://github.com/kentcdodds/use-deep-compare-effect/issues?q=author%3Adaveisfera" title="Bug reports">π</a></td>
|
145 | <td align="center"><a href="https://github.com/samuel-knutson"><img src="https://avatars0.githubusercontent.com/u/2458585?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Sam Knutson</b></sub></a><br /><a href="https://github.com/kentcdodds/use-deep-compare-effect/commits?author=samuel-knutson" title="Documentation">π</a></td>
|
146 | <td align="center"><a href="https://albertlucianto.github.io"><img src="https://avatars0.githubusercontent.com/u/23165866?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Albert Lucianto</b></sub></a><br /><a href="https://github.com/kentcdodds/use-deep-compare-effect/issues?q=author%3AAlbertLucianto" title="Bug reports">π</a> <a href="https://github.com/kentcdodds/use-deep-compare-effect/commits?author=AlbertLucianto" title="Code">π»</a> <a href="https://github.com/kentcdodds/use-deep-compare-effect/commits?author=AlbertLucianto" title="Tests">β οΈ</a></td>
|
147 | <td align="center"><a href="https://github.com/jasperck"><img src="https://avatars.githubusercontent.com/u/5002476?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jasper Chang</b></sub></a><br /><a href="https://github.com/kentcdodds/use-deep-compare-effect/commits?author=jasperck" title="Documentation">π</a></td>
|
148 | <td align="center"><a href="https://github.com/cvolant"><img src="https://avatars.githubusercontent.com/u/37238472?v=4?s=100" width="100px;" alt=""/><br /><sub><b>cvolant</b></sub></a><br /><a href="https://github.com/kentcdodds/use-deep-compare-effect/commits?author=cvolant" title="Code">π»</a></td>
|
149 | </tr>
|
150 | </table>
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | This project follows the [all-contributors][all-contributors] specification.
|
158 | Contributions of any kind welcome!
|
159 |
|
160 | ## LICENSE
|
161 |
|
162 | MIT
|
163 |
|
164 |
|
165 | [npm]: https://www.npmjs.com
|
166 | [node]: https://nodejs.org
|
167 | [build-badge]: https://img.shields.io/github/workflow/status/kentcdodds/use-deep-compare-effect/validate?logo=github&style=flat-square
|
168 | [build]: https://github.com/kentcdodds/use-deep-compare-effect/actions?query=workflow%3Avalidate
|
169 | [coverage-badge]: https://img.shields.io/codecov/c/github/kentcdodds/use-deep-compare-effect.svg?style=flat-square
|
170 | [coverage]: https://codecov.io/github/kentcdodds/use-deep-compare-effect
|
171 | [version-badge]: https://img.shields.io/npm/v/use-deep-compare-effect.svg?style=flat-square
|
172 | [package]: https://www.npmjs.com/package/use-deep-compare-effect
|
173 | [downloads-badge]: https://img.shields.io/npm/dm/use-deep-compare-effect.svg?style=flat-square
|
174 | [npmtrends]: http://www.npmtrends.com/use-deep-compare-effect
|
175 | [license-badge]: https://img.shields.io/npm/l/use-deep-compare-effect.svg?style=flat-square
|
176 | [license]: https://github.com/kentcdodds/use-deep-compare-effect/blob/master/LICENSE
|
177 | [prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
|
178 | [prs]: http://makeapullrequest.com
|
179 | [coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square
|
180 | [coc]: https://github.com/kentcdodds/use-deep-compare-effect/blob/master/CODE_OF_CONDUCT.md
|
181 | [emojis]: https://github.com/all-contributors/all-contributors#emoji-key
|
182 | [all-contributors]: https://github.com/all-contributors/all-contributors
|
183 | [all-contributors-badge]: https://img.shields.io/github/all-contributors/kentcdodds/use-deep-compare-effect?color=orange&style=flat-square
|
184 | [bugs]: https://github.com/kentcdodds/use-deep-compare-effect/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Acreated-desc+label%3Abug
|
185 | [requests]: https://github.com/kentcdodds/use-deep-compare-effect/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement
|
186 | [good-first-issue]: https://github.com/kentcdodds/use-deep-compare-effect/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement+label%3A%22good+first+issue%22
|
187 | [react-hooks]: https://reactjs.org/docs/hooks-effect.html
|
188 | [object-is]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
|
189 |
|