Skip to content

RFC: Provide a React hook for using ratoms/reactions as part of public API #545

Open
@lilactown

Description

@lilactown

I've been working on a project for some time that integrates our existing reagent/re-frame application with some plain react components developed using helix. In order to do this, I had to do a lot of work to write an integration between reagent's ratoms/reactions and React hooks. I essentially have a custom hook called use-reaction that looks like:

(def db (r/atom {}))

;; somewhere in a React component
(let [db (use-reaction db) ;; subscribe to db and re-render on any changes
        ,,,]
   ,,,)
;; create a reaction that updates anytime `(:counter @db)` changes and re-render anytime it changes
(let [counter (use-reaction
               (react/useMemo
                #(r/reaction (:counter @db))
                #js []))]
   ,,,)

Currently this relies on a lot of internal Reagent machinery in order to properly set up watching and disposing the reaction. I think it would be appropriate to add such an integration point to Reagent proper, so that it can change along with any internal changes.

Would the maintainers be interested in accepting a PR for this?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions