Use DidMount for component mount order

WillMount lifecycle method is being deprecated. DidMount can also be
used to capture the mount order. WillMount is called first for top-most
component and last for the children. DidMount is the reverse, first for
children and last the top-most component.
This commit is contained in:
Juho Teperi 2019-08-13 09:32:11 +03:00
parent 2f9e91d697
commit 35ff5d33dd
2 changed files with 18 additions and 22 deletions

View File

@ -23,9 +23,14 @@
(.-msRequestAnimationFrame w) (.-msRequestAnimationFrame w)
fake-raf)))) fake-raf))))
(defn compare-mount-order [c1 c2] (defn compare-mount-order
(- (.-cljsMountOrder c1) [c1 c2]
(.-cljsMountOrder c2))) ;; Mount order is now set in DidMount method. I.e. the
;; top-most component is mounted last and gets largest
;; number. This is reverse compared to WillMount where method
;; for top component gets called first.
(- (.-cljsMountOrder c2)
(.-cljsMountOrder c1)))
(defn run-queue [a] (defn run-queue [a]
;; sort components by mount order, to make sure parents ;; sort components by mount order, to make sure parents

View File

@ -211,22 +211,21 @@
;; Deprecated - warning in 16.9 will work through 17.x ;; Deprecated - warning in 16.9 will work through 17.x
:componentWillMount :componentWillMount
(fn componentWillMount [] (fn componentWillMount []
(this-as c (this-as c (.call f c c)))
(set! (.-cljsMountOrder c) (batch/next-mount-count))
(when-not (nil? f)
(.call f c c))))
;; Deprecated - will work in 17.x ;; Deprecated - will work in 17.x
:UNSAFE_componentWillMount :UNSAFE_componentWillMount
(fn componentWillMount [] (fn componentWillMount []
(this-as c (this-as c (.call f c c)))
(set! (.-cljsMountOrder c) (batch/next-mount-count))
(when-not (nil? f)
(.call f c c))))
:componentDidMount :componentDidMount
(fn componentDidMount [] (fn componentDidMount []
(this-as c (.call f c c))) (this-as c
;; This method is called after everything inside the
;; has been mounted. This is reverse compared to WillMount.
(set! (.-cljsMountOrder c) (batch/next-mount-count))
(when-not (nil? f)
(.call f c c))))
:componentWillUnmount :componentWillUnmount
(fn componentWillUnmount [] (fn componentWillUnmount []
@ -251,8 +250,7 @@
;; Though the value is nil here, the wrapper function will be ;; Though the value is nil here, the wrapper function will be
;; added to class to manage Reagent ratom lifecycle. ;; added to class to manage Reagent ratom lifecycle.
(def obligatory {:shouldComponentUpdate nil (def obligatory {:shouldComponentUpdate nil
;; Handled in add-oblifatory fn :componentDidMount nil
; :componentWillMount nil
:componentWillUnmount nil}) :componentWillUnmount nil})
(def dash-to-method-name (util/memoize-1 util/dash-to-method-name)) (def dash-to-method-name (util/memoize-1 util/dash-to-method-name))
@ -263,14 +261,7 @@
{} fun-map)) {} fun-map))
(defn add-obligatory [fun-map] (defn add-obligatory [fun-map]
(merge obligatory (merge obligatory fun-map))
;; If fun map contains the old name, without UNSAFE_ prefix, use that
;; name, so user will get a warning. If no method use UNSAFE_ method
;; for Reagent implementation.
(if (contains? fun-map :componentWillMount)
nil
{:UNSAFE_componentWillMount nil})
fun-map))
(defn wrap-funs [fmap] (defn wrap-funs [fmap]
(when (dev?) (when (dev?)