(cursor src path)
Provide a cursor into a Reagent atom.
Behaves like a Reagent atom but focuses updates and derefs to
the specified path within the wrapped Reagent atom. e.g.,
(let [c (cursor ra [:nested :content])]
... @c ;; equivalent to (get-in @ra [:nested :content])
... (reset! c 42) ;; equivalent to (swap! ra assoc-in [:nested :content] 42)
... (swap! c inc) ;; equivalence to (swap! ra update-in [:nested :content] inc)
)
The first parameter can also be a function, that should look
something like this:
(defn set-get
([k] (get-in @state k))
([k v] (swap! state assoc-in k v)))
The function will be called with one argument – the path passed to
cursor – when the cursor is deref'ed, and two arguments (path and
new value) when the cursor is modified.
Given that set-get function, (and that state is a Reagent atom, or
another cursor) these cursors are equivalent:
(cursor state [:foo]) and (cursor set-get [:foo]).
Note that a cursor is lazy: its value will not change until it is
used. This may be noticed with add-watch.
Source
(defn
cursor
"Provide a cursor into a Reagent atom.\n\n Behaves like a Reagent atom but focuses updates and derefs to\n the specified path within the wrapped Reagent atom. e.g.,\n (let [c (cursor ra [:nested :content])]\n ... @c ;; equivalent to (get-in @ra [:nested :content])\n ... (reset! c 42) ;; equivalent to (swap! ra assoc-in [:nested :content] 42)\n ... (swap! c inc) ;; equivalence to (swap! ra update-in [:nested :content] inc)\n )\n\n The first parameter can also be a function, that should look\n something like this:\n\n (defn set-get\n ([k] (get-in @state k))\n ([k v] (swap! state assoc-in k v)))\n\n The function will be called with one argument – the path passed to\n cursor – when the cursor is deref'ed, and two arguments (path and\n new value) when the cursor is modified.\n\n Given that set-get function, (and that state is a Reagent atom, or\n another cursor) these cursors are equivalent:\n (cursor state [:foo]) and (cursor set-get [:foo]).\n\n Note that a cursor is lazy: its value will not change until it is\n used. This may be noticed with add-watch."
([src path] (ratom/cursor src path)))