Can we add labelled hooks to dioxus #4048
NikhilNathanael
started this conversation in
Ideas
Replies: 1 comment 4 replies
-
You can implement a dyn compatible version of almost any trait. You could implement labeled hooks in user land by lazily injecting a hashmap into each scope with the hash and value of each hook. This isn't the default strategy dioxus uses to keep track of hooks because labels add a lot of boilerplate to each hook call. If you want to experiment with the approach, you could try creating a library for labeled hooks in dioxus |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
To preface, I'm not an expert in either React or Dioxus so please correct me if i get something.
Dioxus seems to have lifted the rules of hooks directly from React.
Rules of Hooks from React
Rules of Hooks from dioxus docs
The reason we can't simply allow users to provide a label along with the initializer closure is because such a label would have to be type-erased and Hash, Ord, and Eq (for HashMap or BTreeMap) are not dyn compatible.
Making a dyn-compatible version of Eq is not too hard. This is all the code you need.
Implementing a dyn compatible version of Hash is not so trivial but the crate dyn_hash already does this.
You can have type erased labels with the above two traits, and a common subtrait to specifically denote labels for hooks (as opposed to other kinds of labels). This allows a dyn HookLabel to be the key of a HashMap. (i.e. HashMap<dyn HookLabel, dyn Any>)
This implementation of labels seems alright to me, but i was wondering if there were any obvious issues with this that i might have missed.
Note: This idea was inspired by bevy's use of type erased labels (see ScheduleLabel, AppLabel, RenderLabel, etc). My suggestion differs in that it uses trait-upcasting to make the label trait simply a marker trait, but this has an MSRV of 1.86, which may not be desirable.
Beta Was this translation helpful? Give feedback.
All reactions