Last active
August 7, 2023 22:32
-
-
Save shivekkhurana/433e3e130bb1c02df8bff7dfc4538a2c to your computer and use it in GitHub Desktop.
Using React hooks with Reagent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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