cjohansen / portfolio

Eclipse Public License 1.0
236 stars 14 forks source link

Closed over content for Reagent form-2 component persists between tab clicks #15

Closed Reefersleep closed 10 months ago

Reefersleep commented 10 months ago

If the same Reagent form-2 is used in two different scenes, and you click back and forth between their tabs, the closed over data is retained between clicks.

(ns demo.form-2-not-switching-demo
  (:require [portfolio.reagent :refer-macros [defscene]]))

(defn my-form2-component [num]
  ;;This is a silly, non-real-world example.
  ;;Normally, you'd let a r/atom to have local state,
  ;;or maybe trigger side effects that should only
  ;;happen on-mount.
  ;;But nothing more than this is needed
  ;;To demonstrate the problem.
  (let [result (+ 5 num)]
    (fn [_]
      [:div result])))

(defscene first-demo
  :params 1
  [params]
  [my-form2-component params])

(defscene second-demo
  :params 2
  [params]
  [my-form2-component params])

;;Now, click each tab in succession.
;;The displayed number will remain the same.