Skip to content

Instantly share code, notes, and snippets.

@martinklepsch
Created April 14, 2020 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinklepsch/18e1195e09365c54547e22e36481812a to your computer and use it in GitHub Desktop.
Save martinklepsch/18e1195e09365c54547e22e36481812a to your computer and use it in GitHub Desktop.
(defn log-lifecyle [component-name]
(into {}
(map (fn [lifecycle]
[lifecycle (fn [state] (js/console.log component-name lifecycle) state)]))
[:will-mount :will-unmount :did-mount :did-unmount]))
(rum/defc A < (log-lifecyle :A)
[]
[:span "A"])
(rum/defc B < (log-lifecyle :B)
[]
[:span "B"])
(rum/defcs LifecycleOrderTestApp
< (rum/local true :component)
[s]
[:div
(case @(:component s)
true (A)
false (B))
[:div
[:button {:on-click #(swap! (:component s) not)} "swap components"]]])
;; render A
:A :will-mount
:A :did-mount
;; replacing A with B
:B :will-mount
:A :will-unmount
:B :did-mount
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment