Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

use-memo and use-callback via metadata #48

Open
lilactown opened this issue May 5, 2020 · 2 comments
Open

use-memo and use-callback via metadata #48

lilactown opened this issue May 5, 2020 · 2 comments

Comments

@lilactown
Copy link
Owner

lilactown commented May 5, 2020

We should be able to signal that an expression should be wrapped in a use-memo or use-callback by annotating it with metadata in the body of a defnc or a defhook.

(defnc my-component
  [{:keys [value]}]
  ^:memo (d/div value)) ;; returns the same react element for the save `value`

^:memo and ^:callback metadata should tell defnc and defhook to emit a use-memo or use-callback hook that will automatically fill dependencies using the same algorithm as :auto-deps.

A dependency seq can also be provided to override this behavior:

(defnc my-component
  [{:keys [foo bar baz]}]
  ^{:memo [foo]} (d/div value)) ;; ignores changes to `bar` and `baz`

^:memo and ^:callback metadata should be usable in let bindings:

(defnc my-component
  [{:keys [value]}]
  (let [calculated ^:memo (+ value 10)
        on-click ^:callback #(js/alert calculated)]
    ^:memo (d/button {:on-click on-click} "Click me!")))

^:memo and ^:callback should allow enforcement of the Rules of Hooks checks that are currently behind an experimental feature flag.

@rome-user
Copy link
Contributor

Is there something missing in the latest version of Helix? I know that defnc exposes ^:memo and ^:callback as metadata optimizations. I'm not sure about defhook.

@lilactown
Copy link
Owner Author

Right now, defnc implements this behind a feature flag you may enable. defhook does not yet support it.

I haven't fully tested it yet in production, which is why it's still behind a feature flag. In general I'm still a little unsure about using metadata to manage these type of semantics. I also want to ensure that it will emit compile warnings when breaking the rules of hooks.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants