What is the equivalent of useRef
in Leptos?
#880
-
Trying to solve this question I started to wonder how to store mutable component-scoped data that is not really a signal, in this case a |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
So it's important to start with the question, "Why on earth does React have I'm not a React expert by any means, by as I understand it, the reason React needs Leptos doesn't have this problem: The body of the component function only runs once, so all the normal rules of Rust ownership and borrowing apply exactly as you'd expect. However, it does still need some kind of wrapper because of the interaction between Rust's ownership and movement rules and the fact that any closures that will be shipped to JavaScript or stored in the reactive system need to be If you need to share ownership of an object across two closures or between a closure and the world outside the closure, etc., this means you'd typically wrap it in an Leptos also provides a Per your comment in the other question, to run something when the component is removed you can use the |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot for the explanation! It is indeed an interesting question why React needs What still causes me some headaches is the semantics of I vaguely remember from solid-js that e.g. effects could always return another callback, which serves as the corresponding cleanup side effect of that particular effect. If I remember correctly it was guaranteed that these effects run on the component level, making cleanup on unmount very natural. Would it make sense to offer a similar higher-level cleanup functionality? EDIT: Perhaps I just ran into #802? |
Beta Was this translation helpful? Give feedback.
So it's important to start with the question, "Why on earth does React have
useRef
anyway?" In other words, whyuseRef
instead of a normal variable?I'm not a React expert by any means, by as I understand it, the reason React needs
useRef
is that the body of the component function runs over and over again whenever state is changed. This means that anything stored in a normallet
orconst
variable inside the body of the component function is created again and again.useRef
exists to tell React "no, don't create this object again, actually looked in your collection of stored stable references and use the thing you created the first time you rendered this function."Leptos doesn't have this …