I store it in an atom 🙂
I use plain reagent for 'intra-component' state
and yeah as it turns out, that's the problem
reagent doesn't reset *info
when I reload
I had the same problem with the error-boundary component you proposed, because it also keeps state in an atom
I'm honestly kind of stumped as to how I should approach this
basically the call order seems to be
1. get-derived-state-from-error
2. render
3. component-did-catch
I want to use the info
arg, that's only provided to component-did-catch
to trigger a re-render (so I can display that state)
is there a way to do this without wrapping the component in (let [*info (r/atom nil)] )
?
alternatively, is there a way to force that atom to be cleared when the page gets reloaded?
....okay, I finally got it working the way I want
(defn <error-boundary>
"Util for inserting an error boundaries in the React tree. When a child throws
an error, just renders that error instead of crashing."
[& _]
(let [*error (atom nil)]
(r/create-class
{:component-did-catch (fn [this e i]
(reset! *error [e (.-componentStack i)])
(.forceUpdate this))
:display-name "error-boundary"
:get-derived-state-from-error #(reset! *error [%])
:render #(r/as-element
(if-let [[error stack] @*error]
[<error-message> error stack]
(into [:<>] (r/children %))))})))
plain old non-reagent atoms (and forceUpdate
) to the rescue 🙂