-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Integrate use_ref with NodeRef #1642
Comments
Can this issue be closed since it seems like it works already? |
It's still unclear what is actually desired here. The entire hook API is a bit all over the place so I believe this issue is justified to discuss a better approach to this. |
@hamza1311 my intent was just to ensure that |
It works but it's not very ergonomic to use Rc<RefCell<Rc<RefCell<Rc<RefCell<NodeRefInner>>>>>>
^^^^^^^^^^^^^^^^^^^^^^^^^ NodeRef part Which is lots of interior mutability 👀 Might be worth biting the bullet here and just making a specific hook for pub fn use_node_ref() -> NodeRef {
use_hook(NodeRef::default, |state, _| state.clone(), |_| {})
} Then the example above would look like this: #[function_component(UseRef)]
fn ref_hook() -> Html {
let outer_html = use_state(|| "".to_string());
let node_ref = use_node_ref();
let onclick = {
let node_ref = node_ref.clone();
let outer_html = outer_html.clone();
Callback::from(move |_| {
let to_set = node_ref
.cast::<yew::web_sys::Element>()
.unwrap()
.outer_html();
outer_html.set(to_set)
})
};
html! {
<>
<div id="result" ref={node_ref}>{ "Filler" }</div>
{(*outer_html).clone()}
<br />
<button {onclick}>{"Refresh"}</button>
</>
}
} syntax updated to work on Just thought I'd add this into the mix :) |
I had a thought of having a trait do the trick. |
If you could manage it then that would be even better than having a separate hook :) |
I've been playing around with this bit more and I do think that without specialization integrating I think it will be quite common for users to want to have multiple // type can be inferred but added for clarity :)
let [a, b, c]: &[NodeRef; 3] = &*use_node_ref(); where pub fn use_node_ref<const N: usize>() -> Rc<[NodeRef; N]>; The The downside to this is that currently it requires a sprinkling of unsafe to achieve - there are actually quite a few ways to get this same effect but most of the safe ones are currently hiding in nightly 🙃.. One way but isn't quite as ergonomic is: let [a, b, c] = &*use_node_ref(Default::default); where pub fn use_node_ref<const N: usize>(init: impl FnOnce() -> [NodeRef; N]) -> Rc<[NodeRef; N]>; Thought I'd add these points into consideration too :) |
How is pub fn use_node_ref<const N: usize>() -> Rc<[NodeRef; N]>; better than pub fn use_node_ref() -> NodeRef; The hook can be called multiple times to create new node refs. It can be implemented without nightly or unsafe like you described in another comment above. Also, when we add that hook, it would be better to rename |
It was just another idea really and wasn't sure how often people use multiple
Agreed, prefer |
So if |
This was initially posted in #1129. This issue tracks its progress.
I'd love to have what @jstarry meant by this "integration" explained here because as it stands,
use_ref
works well withNodeRef
. Example:Here's the output displayed after clicking on "Refresh" button:
The text was updated successfully, but these errors were encountered: