Skip to content

Commit 8cef855

Browse files
mkphilippamarkovics
authored andcommitted
Make use-promise hook react to changes in promise
1 parent 4f46658 commit 8cef855

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/nextjournal/clerk/render/hooks.cljs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,16 @@
138138
"React hook which resolves a promise and handles errors."
139139
[p]
140140
(let [handle-error (use-error-handler)
141+
!latest-promise (use-ref p)
141142
!state (use-state nil)]
142143
(use-effect (fn []
144+
(reset! !latest-promise p)
143145
(-> p
144-
(.then #(reset! !state %))
145-
(.catch handle-error)))
146-
#js [])
146+
(.then #(when (= p @!latest-promise)
147+
(reset! !state %)))
148+
(.catch #(when (= p @!latest-promise)
149+
(handle-error %)))))
150+
#js [p])
147151
@!state))
148152

149153
(defn ^js use-d3-require [package]

0 commit comments

Comments
 (0)