Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```clj
- (defn anonymous-component-example-var-bound-child [text]
- (js/console.log "RENDER" text)
- [:div text])
- (defn anonymous-component-example []
- (let [*state (reagent/atom 0)]
- (js/console.log "CREATE parent")
- (fn []
- (js/console.log "RENDER parent")
- (let [state @*state]
- [:div.parent
- ;; var-bound component doesn't re-rendered when argument doesn't changed
- [anonymous-component-example-var-bound-child "var-bound child"]
- ;; changing key forces re-rendering of var-bound component even when argument doesn't changed
- ^{:key state}
- [anonymous-component-example-var-bound-child "var-bound child with changing key"]
- ;; anonymous component re-rendered even when argument doesn't changed
- [(fn [text]
- (js/console.log "RENDER anonymous child")
- [:div text])
- "anonymous child"]
- ;; constant key can't prevent re-rendering of anonymous component
- ^{:key "foo"}
- [(fn [text]
- (js/console.log "RENDER anonymous child with constant key")
- [:div text])
- "anonymous child with constant key"]
- [:br]
- [:div "State:" state]
- [:button
- {:on-click
- (fn [_]
- (js/console.log "-- clicked --")
- (swap! *state inc))}
- "Click me!"]]))))
- ```
- Devtools console output after clicking button three times
- ```
- CREATE parent
- RENDER parent
- RENDER var-bound child
- RENDER var-bound child with changing key
- RENDER anonymous child
- RENDER anonymous child with constant key
- -- clicked --
- RENDER parent
- RENDER var-bound child with changing key
- RENDER anonymous child
- RENDER anonymous child with constant key
- -- clicked --
- RENDER parent
- RENDER var-bound child with changing key
- RENDER anonymous child
- RENDER anonymous child with constant key
- -- clicked --
- RENDER parent
- RENDER var-bound child with changing key
- RENDER anonymous child
- RENDER anonymous child with constant key
- ```
Add Comment
Please, Sign In to add comment