From 35ff5d33dd9dda6ac4103bb1bcb88580bdd12ddd Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Tue, 13 Aug 2019 09:32:11 +0300 Subject: [PATCH] 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. --- src/reagent/impl/batching.cljs | 11 ++++++++--- src/reagent/impl/component.cljs | 29 ++++++++++------------------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/reagent/impl/batching.cljs b/src/reagent/impl/batching.cljs index 81385d8..5282c81 100644 --- a/src/reagent/impl/batching.cljs +++ b/src/reagent/impl/batching.cljs @@ -23,9 +23,14 @@ (.-msRequestAnimationFrame w) fake-raf)))) -(defn compare-mount-order [c1 c2] - (- (.-cljsMountOrder c1) - (.-cljsMountOrder c2))) +(defn compare-mount-order + [c1 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] ;; sort components by mount order, to make sure parents diff --git a/src/reagent/impl/component.cljs b/src/reagent/impl/component.cljs index 916081e..9d3ee18 100644 --- a/src/reagent/impl/component.cljs +++ b/src/reagent/impl/component.cljs @@ -211,22 +211,21 @@ ;; Deprecated - warning in 16.9 will work through 17.x :componentWillMount (fn componentWillMount [] - (this-as c - (set! (.-cljsMountOrder c) (batch/next-mount-count)) - (when-not (nil? f) - (.call f c c)))) + (this-as c (.call f c c))) ;; Deprecated - will work in 17.x :UNSAFE_componentWillMount (fn componentWillMount [] - (this-as c - (set! (.-cljsMountOrder c) (batch/next-mount-count)) - (when-not (nil? f) - (.call f c c)))) + (this-as c (.call f c c))) :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 (fn componentWillUnmount [] @@ -251,8 +250,7 @@ ;; Though the value is nil here, the wrapper function will be ;; added to class to manage Reagent ratom lifecycle. (def obligatory {:shouldComponentUpdate nil - ;; Handled in add-oblifatory fn - ; :componentWillMount nil + :componentDidMount nil :componentWillUnmount nil}) (def dash-to-method-name (util/memoize-1 util/dash-to-method-name)) @@ -263,14 +261,7 @@ {} fun-map)) (defn add-obligatory [fun-map] - (merge obligatory - ;; 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)) + (merge obligatory fun-map)) (defn wrap-funs [fmap] (when (dev?)