Replies: 3 comments 8 replies
-
Thank you for writing all this down. Looks good, cant wait for the future PR! |
Beta Was this translation helpful? Give feedback.
-
I like this approach. We can even sneak in One implementation detail to note here is that all of the hooks tests will need to be refactored as they don't use the proc-macro. |
Beta Was this translation helpful? Give feedback.
-
Random little idea: How about rewriting hook calls so that they create a That would allow them to be freely placed and would alleviate the need to move the calls. self-written hooks would do the same, and there would be a sort of "hook TypeId-Marker stack" |
Beta Was this translation helpful? Give feedback.
-
I am creating this as a discussion as I need to prioritise SSR Hydration and I would appreciate some feedback before working on this.
Rationale
The Function Component and Hooks v2 tries to solve the following issues:
(The calling position lint should apply to not only inside of function components, but also inside of user defined hooks)
use_
prefix is not enforced.There're still issues to be solved (mainly around macros) but in general should be an improvement over the issues stated above.
Design
The following trait will be added:
To define a hook, one must use the
#[hook]
attribute macro:The
#[hook]
macro rewrites the hook to: (similar to howasync fn
works)This renders hook calls outside of a function component or user defined hook useless as a hook is not run until
run
is called and it's not possible to acquireFunctionComponentState
without the help of procedural macros.When hooks are used inside a function component or a user defined hook,
#[function_component]
or#[hook]
macro checks if it's possible to call the hook in its defined position, it will output a compile error if it's not possible to call a hook in that position.If it's possible, the hook call will be rewritten:
state
is only acquired once as a mutable reference in thefn run
of a function component and will be defined withSpan::mixed_site()
so it's not accessible from outside of the procedural macro.How to handle macros
This design has elected to not apply any special handling with macros (with the exception to the macro-style hooks) until
proc_macro_expand
stabilises.Hence, it's currently possible to write code like the following:
See: rust-lang/rust#90765
Compatibility with macro-style hooks
This design also is also compatible with macro-style hooks.
The only requirement is that the macro-style hook must define a normal hook call at the end of their emitted token stream so they can be evaluated like function-based macros:
Beta Was this translation helpful? Give feedback.
All reactions