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:
([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.