-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
16 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,21 @@ | |
([f] | ||
(react/useEffect f)) | ||
([f deps] | ||
(react/useEffect f (to-array deps)))) | ||
;; React uses JS equality to check of the current deps are different than | ||
;; previous deps values. This means that CLJS data (e.g. maps, sets, vecs) | ||
;; equality is not respected and will trigger if you e.g. pass in a vec of | ||
;; strings as props and need to depend on that inside of an effect. | ||
;; | ||
;; We can work around this by assigning the previous deps to a ref, and do | ||
;; our own equality check to see if they have changed. If so, we update the | ||
;; ref to equal the current value. | ||
;; | ||
;; We can then just pass this one value into `useEffect` and it will only | ||
;; change if Clojure's equality detects a difference. | ||
(let [-deps (react/useRef deps)] | ||
(when (not= deps (.-current -deps)) | ||
(set! (.-current -deps) deps)) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
lilactown
Author
Collaborator
|
||
(react/useEffect f #js [(.-current -deps)])))) | ||
|
||
(def <-context | ||
"Just react/useContext" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Just wanted to point out that in React's docs they don't recommend setting ref during render function call because it can lead to unexpected behavior.
https://reactjs.org/docs/hooks-faq.html#is-there-something-like-instance-variables