Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(ns whatever.sub-test
(:require [reagent.core :as r]
[reagent.ratom :as ratom]
[re-frame.core :as rf]))
(def invalidate-form
(rf/->interceptor
{:before identity
:after (fn [{{:keys [:db]} :effects :as ctx}]
(assoc-in ctx [:effects :db :test-changed] true))}))
(rf/reg-sub
:test
(fn [db] (:test db)))
(rf/reg-event-db
:set-test
[invalidate-form]
(fn [db [_ newval]] (assoc db :test newval)))
(rf/reg-event-db
:set-test-changed
(fn [db [_ newval]]
(assoc db :test-changed newval)))
(rf/reg-sub
:test-changed
(fn [db _] (:test-changed db)))
(rf/reg-event-db
:change-d
[invalidate-form]
(fn [db [_ newd]]
(assoc-in db [:test :b 1 :d] newd)))
(defn re-frame-idiomatic-games []
(let [changed (rf/subscribe [:test-changed])
test (rf/subscribe [:test])]
[:div
[:button {:on-click #(rf/dispatch [:set-test {:a 1 :b [{:c 3} {:d 4}]}])}
"Set initial state"]
[:button {:on-click #(rf/dispatch [:change-d (rand-int 10)])}
"Change field in :test randomly"]
[:button {:on-click #(rf/dispatch [:set-test-changed false])}
"Reset :test-changed"]
[:div ":test " (str @test)]
[:div ":test-changed " (str @changed)]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment