-
-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Monadic hooks #1101
Monadic hooks #1101
Conversation
This reverts commit 05f468c. The repository secret has been updated with a renewed key.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The syntax is a great improvement. It will be easier to use and much easier to create custom hooks. 🎊 Should Hooks.md
be updated? We probably need the existing documentation for awhile for existing code, but it could be at the bottom and marked as legacy? I don't see why someone would want to develop new code with the old syntax once this is available. 😄
library/ghpages/src/main/scala/ghpages/examples/HooksExample.scala
Outdated
Show resolved
Hide resolved
Since this a beta branch, I was planning to give this a bit more of a test before officially documenting it, in case there are changes. After we have it in the wild for some time, we can decide how to document both styles. |
library/coreGeneric/src/main/scala/japgolly/scalajs/react/hooks/react18.scala
Outdated
Show resolved
Hide resolved
library/coreGeneric/src/main/scala/japgolly/scalajs/react/hooks/HookResult.scala
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like a monadic approach was considered in the early days but there was a significant disadvantage to it. Something about safety that was lost (meaning there was a React-imposed requirement that I was able to enforce at the compiler-level with the existing approach, but would become a potential user-error with a mondaic approach)? I don't remember with enough detail. Looking at the code though, this monadic approach looks pretty good. In fact, it looks very nice. 👍
Impressive!
Only other two concerns are 1) documentation update (as you've already mentioned), and 2) binary backwards-compatibility — could I hear your position on it breaking? How concerned are you? Why / why not?
I've been trying to remember if we considered this, but can't remember either. We probably considered it but didn't consider delaying evaluation, which is what makes it safe. That said, you can still break the rules (eg:
Thank you!
I'll add to the PR. Not sure how to handle the availability of both styles.
If we make this part of a major release ( |
Ended up renaming to Also documented the new style as the default style for hooks, making it explicit that there are ways around the rules; while keeping the builder-style as an alternative that provide complete safety while being more verbose. Finally, a straightforward way to wrap hook facades was added. |
library/tests/src/test/scala/japgolly/scalajs/react/core/HooksTest.scala
Show resolved
Hide resolved
library/coreGeneric/src/main/scala/japgolly/scalajs/react/component/Delayed.scala
Outdated
Show resolved
Hide resolved
library/coreGeneric/src/main/scala/japgolly/scalajs/react/hooks/package.scala
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reviewed, feedback above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Nice work!
Implements an alternative syntax for hooks, in the form of:
This provides a simpler syntax, while still guaranteeing safeguards to honor the rules of hooks: hook invocations are delayed and can only be invoked by
ScalaFnComponent
.Source compatibility is preserved with the builder-style hooks.