Guest User

Untitled

a guest
Sep 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. ```clj
  2. (defn anonymous-component-example-var-bound-child [text]
  3. (js/console.log "RENDER" text)
  4. [:div text])
  5.  
  6. (defn anonymous-component-example []
  7. (let [*state (reagent/atom 0)]
  8. (js/console.log "CREATE parent")
  9. (fn []
  10. (js/console.log "RENDER parent")
  11. (let [state @*state]
  12. [:div.parent
  13.  
  14. ;; var-bound component doesn't re-rendered when argument doesn't changed
  15. [anonymous-component-example-var-bound-child "var-bound child"]
  16.  
  17. ;; changing key forces re-rendering of var-bound component even when argument doesn't changed
  18. ^{:key state}
  19. [anonymous-component-example-var-bound-child "var-bound child with changing key"]
  20.  
  21. ;; anonymous component re-rendered even when argument doesn't changed
  22. [(fn [text]
  23. (js/console.log "RENDER anonymous child")
  24. [:div text])
  25. "anonymous child"]
  26.  
  27. ;; constant key can't prevent re-rendering of anonymous component
  28. ^{:key "foo"}
  29. [(fn [text]
  30. (js/console.log "RENDER anonymous child with constant key")
  31. [:div text])
  32. "anonymous child with constant key"]
  33.  
  34. [:br]
  35. [:div "State:" state]
  36. [:button
  37. {:on-click
  38. (fn [_]
  39. (js/console.log "-- clicked --")
  40. (swap! *state inc))}
  41. "Click me!"]]))))
  42. ```
  43. Devtools console output after clicking button three times
  44. ```
  45. CREATE parent
  46. RENDER parent
  47. RENDER var-bound child
  48. RENDER var-bound child with changing key
  49. RENDER anonymous child
  50. RENDER anonymous child with constant key
  51. -- clicked --
  52. RENDER parent
  53. RENDER var-bound child with changing key
  54. RENDER anonymous child
  55. RENDER anonymous child with constant key
  56. -- clicked --
  57. RENDER parent
  58. RENDER var-bound child with changing key
  59. RENDER anonymous child
  60. RENDER anonymous child with constant key
  61. -- clicked --
  62. RENDER parent
  63. RENDER var-bound child with changing key
  64. RENDER anonymous child
  65. RENDER anonymous child with constant key
  66. ```
Add Comment
Please, Sign In to add comment