-
Notifications
You must be signed in to change notification settings - Fork 437
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
RFC: Typesafe "heterogenous" dependency arrays for React hooks #6502
Conversation
This seems be working based on the textual match in AST. In case of newly value binding would not work. e.g. |
Previous discussion for reference: rescript-lang/rescript-react#97 |
The PR has two purposes:
I do not see how either of these could be accomplished via a wrapper function. |
Downside of manipulating the expression directly based on the textual match. Instead, we can use the attribute such as useEffect(_ => None, @deps [1, "2"]) // something like The built-in ppx would transform the array, maybe like
IMHO, this is more typesafe. |
@mununki That's also a possible, and more explicit approach, and I agree that it makes it clearer that some "magic" is happening here with that "heterogenous" array. The downside is that having to add Also, while @zth What are your thoughts on this? |
Sorry, I missed this. I like the general idea of making this part of using React simpler/more intuitive, but I'm also afraid of making arrays appear heterogenous. Pretty sure that'll lead to confusion about why it's not allowed anywhere else. I had one idea related to this, that I'm not sure if it's practically feasible, but might be something worth thinking about - what if we introduced either a type, or an annotation, that would let us enforce using a type that has a specific runtime representation? @module("react")
external useMemo: (unit => 'value, @array 'deps) => 'value = "useMemo"
Again, might not be feasible. Me and @cristianoc had a discussion about this but I honestly don't remember what we said. Anyway, the effect would be that we'd get rid of "useN", but we'd still have array vs tuples. So, one less issue, but not everything solved. |
I'd like to have a more general solution |
I quite like the |
Closing this as this approach is too hacky. 🙂 |
There are currently the following issues with dependency arrays for React hooks, for example
useEffect
which is defined as follows:'deps
is actually an array or a tuple.(a, b)
to one, it is easy to just deleteb
and end up with(a)
instead of[a]
which is incorrect and can lead to issues at runtime.Idea:
dependency
.array<dependency>
.In this usage (invocation of React.useEffect/useCallback etc. with an array or tuple as the second parameter), the JSX PPX would e.g. convert
[a, b, c]
toJsxPPXReactSupport.unsafeToDependencyArray((a, b, c))
(with zero cost).This PR is a proof of concept for this idea.