Skip to content

Instantly share code, notes, and snippets.

@shivekkhurana
Last active August 7, 2023 22:32
Show Gist options
  • Save shivekkhurana/433e3e130bb1c02df8bff7dfc4538a2c to your computer and use it in GitHub Desktop.
Save shivekkhurana/433e3e130bb1c02df8bff7dfc4538a2c to your computer and use it in GitHub Desktop.
Using React hooks with Reagent
;; Reagent component that depends on Ratom
(def counter-state (r/atom 0))
(defn counter [params]
[:<>
[:div (str params)]
[:div @counter-state]
[:button {:on-click #(swap! counter-state inc)} "Inc"]])
;; Wrapper component that will be rendered as React element, will call hooks and pass values from hooks to Reagent component
;; currently using `useParams` hook from `react-router-dom`, but any hook can be used
(defn params-container [props]
(let [params (useParams)
children (aget props "children")]
(r/as-element
[children params])))
;; component usage, counter will have access to data from hooks
[:> params-container counter]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment