17 Sep 2023 |
kirillsemyonkin | you dont like sending callbacks down the tree, which i think you mean sending a prop parent -> child -> child -> child - this is called prop drilling, and you may solve it just using built-in ContextProvider
pseudo example code that may not have all derive s needed:
#[derive(Clone, PartialEq)]
pub struct YourContext {
callback: Callback<SomeThing, SomeThing>;
}
impl YourContext {
pub fn callback(&self, thing: SomeThing) -> SomeThing {
self.callback.emit(thing)
}
}
#[function_component]
pub fn Parent(...children...) -> Html {
let callback = ...;
let context = YourContext { callback };
html! {
<ContextProvider<YourContext> { context }>
{ children.clone() }
</ContextProvider>
}
}
#[function_component]
pub fn Child() -> Html {
let context = use_context::<YourContext>().unwrap();
...context.callback(...)...
}
#[function_component]
pub fn Main() -> Html {
html! {
<Parent>
<Child />
</Parent>
}
} | 10:59:31 |
kirillsemyonkin | * you dont like sending callbacks down the tree, which i think you mean sending a prop parent -> child -> child -> child - this is called prop drilling, and you may solve it just using built-in ContextProvider
pseudo example code that may not have all derive s needed:
#[derive(Clone, PartialEq)]
pub struct YourContext {
callback: Callback<SomeThing, SomeThing>;
}
impl YourContext {
pub fn callback(&self, thing: SomeThing) -> SomeThing {
self.callback.emit(thing)
}
}
#[function_component]
pub fn Parent(...children...) -> Html {
let callback = ...;
let context = YourContext { callback };
html! {
<ContextProvider<YourContext> { context }>
{ children.clone() }
</ContextProvider<YourContext>>
}
}
#[function_component]
pub fn Child() -> Html {
let context = use_context::<YourContext>().unwrap();
...context.callback(...)...
}
#[function_component]
pub fn Main() -> Html {
html! {
<Parent>
<Child />
</Parent>
}
} | 10:59:45 |
kirillsemyonkin | https://yew.rs/docs/next/concepts/contexts | 11:00:06 |
kirillsemyonkin | two things here: - callback is not prop-drilled, because we use contexts - child does not know that theres an actual Callback , we hid it from them by using impl YourContext | 11:05:14 |
kirillsemyonkin | also, UseStateSender and UseReducerDispatch are pretty much Callbacks and you may use them as a shortcut | 11:09:34 |
kirillsemyonkin | * also, UseStateSetter and UseReducerDispatch are pretty much Callbacks and you may use them as a shortcut | 11:09:38 |
kraem_ | thanks. i'll have to read up on contexts. | 11:56:16 |
| holg_#0 changed their display name from holg to holg_#0. | 12:05:02 |
18 Sep 2023 |
| @ryanpaaaj:matrix.org joined the room. | 05:16:46 |
| @ryanpaaaj:matrix.org left the room. | 05:51:33 |
| atoktoto joined the room. | 08:41:31 |
| xkrebs_ changed their display name from xkrebs_ to xkrebs_#0. | 11:31:53 |
| xkrebs_ changed their display name from xkrebs_#0 to xkrebs_. | 11:31:54 |
| Minimaul πΊπΈπ³οΈβπ changed their profile picture. | 12:29:02 |
| Minimaul πΊπΈπ³οΈβπ changed their profile picture. | 12:33:51 |
19 Sep 2023 |
| pissmilk#0 changed their profile picture. | 04:53:34 |
| tiresias___ joined the room. | 14:16:19 |
| jias7dp8b71#0 changed their display name from jias7dp8b71 to jias7dp8b71#0. | 15:55:19 |
| @tomidomino:cutefunny.art joined the room. | 19:54:35 |
| @tomidomino:cutefunny.art left the room. | 20:01:09 |
22 Sep 2023 |
| sergioribera#0 changed their display name from Sergio Ribera to sergioribera#0. | 20:30:39 |
| Minimaul πΊπΈπ³οΈβπ changed their profile picture. | 21:24:19 |
| Minimaul πΊπΈπ³οΈβπ changed their profile picture. | 21:26:16 |
23 Sep 2023 |
| @dkmon23:matrix.org left the room. | 10:19:29 |
| Simon changed their display name from siku2#0001 to siku2#0. | 18:04:56 |
| Simon changed their display name from siku2#0 to Simon. | 18:04:59 |
| .xiqq joined the room. | 21:58:57 |
| 0xalcibiades joined the room. | 23:11:51 |
24 Sep 2023 |
| .christopherhunt changed their display name from Christopher Hunt#2382 to .christopherhunt#0. | 04:16:49 |
| .christopherhunt changed their display name from .christopherhunt#0 to .christopherhunt. | 04:16:49 |