Skip to content

Commit 79c5f13

Browse files
authored
Merge pull request reagent-project#373 from reagent-project/danielcompton-not=-throwing
Handle not= exception
2 parents 41ef62f + fe4f348 commit 79c5f13

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Removed component type assertion for `:>` (#[369](https://github.com/reagent-project/reagent/issues/369), [#372](https://github.com/reagent-project/reagent/pull/372)))
1111
- This caused problems with React Context where component is Plain JS object with special properties
1212
- `React/createElement` will still provide error if `:>` is used with invalid values
13+
- Handle exceptions from `not=` in Reagent `component-did-update` method ([#350](https://github.com/reagent-project/reagent/pull/350), [#344](https://github.com/reagent-project/reagent/pull/344))
1314

1415
## 0.8.0 (2018-04-19)
1516

src/reagent/impl/component.cljs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@
167167
new-argv ($ nextprops :argv)
168168
noargv (or (nil? old-argv) (nil? new-argv))]
169169
(cond
170-
(nil? f) (or noargv (not= old-argv new-argv))
170+
(nil? f) (or noargv (try (not= old-argv new-argv)
171+
(catch :default e
172+
(warn "Exception thrown while comparing argv's in shouldComponentUpdate: " old-argv " " new-argv " " e)
173+
false)))
171174
noargv (.call f c c (get-argv c) (props-argv c nextprops))
172175
:else (.call f c c old-argv new-argv))))))
173176

test/reagenttest/testreagent.cljs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,3 +1229,29 @@
12291229
[:> Consumer {}
12301230
(fn [v]
12311231
(r/as-element [:div "Context: " v]))]])))))
1232+
1233+
(deftest on-failed-prop-comparison-in-should-update-swallow-exception-and-do-not-update-component
1234+
(let [prop (r/atom {:todos 1})
1235+
component-was-updated (atom false)
1236+
error-thrown-after-updating-props (atom false)
1237+
component-class (r/create-class {:reagent-render (fn [& args]
1238+
[:div (str (first args))])
1239+
:component-did-update (fn [& args]
1240+
(reset! component-was-updated true))})
1241+
component (fn []
1242+
[component-class @prop])]
1243+
1244+
(when (and isClient (dev?))
1245+
(let [e (debug/track-warnings
1246+
#(with-mounted-component [component]
1247+
(fn [c div]
1248+
(reset! prop (sorted-map 1 2))
1249+
(try
1250+
(r/flush)
1251+
(catch :default e
1252+
(reset! error-thrown-after-updating-props true)))
1253+
1254+
(is (not @component-was-updated))
1255+
(is (not @error-thrown-after-updating-props)))))]
1256+
(is (re-find #"Warning: Exception thrown while comparing argv's in shouldComponentUpdate:"
1257+
(first (:warn e))))))))

0 commit comments

Comments
 (0)