Skip to content

Instantly share code, notes, and snippets.

@olivergeorge
Last active January 18, 2021 13:22
Show Gist options
  • Save olivergeorge/ac9dbda3934de9d215083708501bf42c to your computer and use it in GitHub Desktop.
Save olivergeorge/ac9dbda3934de9d215083708501bf42c to your computer and use it in GitHub Desktop.
Example of adding an error boundary so the app doesn't crash when views are bad (ref: https://reactjs.org/docs/error-boundaries.html)
(ns app.core
(:require [reagent.core :as r]
[interop.react-native :as rn]
[goog.object :as gobject]
[app.cofx]
[app.events]
[app.subs]
[app.fx]
[app.ins]
[app.nav :as nav]
[re-frame.core :as rf]))
(defn error-report [error]
[rn/view {:style {:padding-horizontal 20 :background-color "pink" :flex 1}}
[rn/text {:style {:padding-top 80 :font-size 32 :color "red"}} "Error report"]
[rn/text {:style {:padding-top 20 :font-size 14 :color "black"}} (str error)]
[rn/text {:style {:padding-top 10 :font-size 12 :color "black"}}
(str (gobject/get error "componentStack"))]])
(defn error-boundary []
(let [*error (r/atom nil)]
(r/create-class
{:get-derived-state-from-error (fn [e]
#_(sentry/capture-exception e)
(reset! *error e))
:component-did-catch (fn [_ _ _] goog/DEBUG)
:reagent-render (fn [form] (if @*error [error-report @*error] form))})))
(defonce *bootstrapped? (atom false))
(defn ^:export -main []
(when-not @*bootstrapped?
(reset! *bootstrapped? true)
(rf/dispatch-sync [:app.events/bootstrap]))
(r/as-element [error-boundary [nav/app]]))
; Needed for figwheel repl
(def figwheel-rn-root -main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment