Skip to content

Instantly share code, notes, and snippets.

@olivergeorge
Last active May 10, 2020 20:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olivergeorge/8ec1a8ea45a2a607f43a918127d29fa9 to your computer and use it in GitHub Desktop.
Save olivergeorge/8ec1a8ea45a2a607f43a918127d29fa9 to your computer and use it in GitHub Desktop.
Some notes on React error boundaries

Based on: https://reactjs.org/docs/error-boundaries.html

Error boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed.

As of React 16, errors that were not caught by any error boundary will result in unmounting of the whole React component tree.

Reagent gives us access to the error boundary features

  • Returning true from :component-did-catch stops the unmounting behaviour
  • Set an error flag with :get-derived-state-from-error - intended to set state so you can re-render app in “error mode” but could also be used for other recovery strategies

This is an example which seems to work

(defn error-boundary []
  (let [*error (r/atom nil)]
    (r/create-class
      {:get-derived-state-from-error (fn [e] (reset! *error e))
       :component-did-catch          (fn [_ _ _] true)
       :reagent-render               (fn [form]
                                       (if @*error
                                         [rn/view {:style {:padding 50}}
                                          [rn/text {:style {:font-size 24 :color "red"}} "Error rendering component.  Krell will retry on reload."]
                                          [rn/text {:style {:font-size 12 :color "black"}} (str @*error)]
                                          [rn/text {:style {:font-size 12 :color "black"}} (str (.-componentStack @*error))]]
                                         form))})))
(defn ^:export -main [& args]
  (r/as-element [error-boundary [app]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment