yew icon indicating copy to clipboard operation
yew copied to clipboard

Document how to send messages to children components

Open jstarry opened this issue 5 years ago • 3 comments

Not sure how formal the support for this feature should be, but at the moment is possible using the approach in the nested list example here: https://github.com/yewstack/yew/blob/master/examples/nested_list/src/app.rs

So I think it deserves a write up in the docs

jstarry avatar Jan 31 '20 08:01 jstarry

I wish there was a way that doesn't require us do:

*props.weak_link.borrow_mut() = Some(link);

A cleaner approach is perhaps, when the parent sets a prop named link, same ComponentLink can be passed to the child's create.


I also tried using the props instead, but Clone requirement is the issue there.

muhuk avatar Jun 28 '20 12:06 muhuk

I have found some sort of a solution that works in my use case. I need to pass a non-cloneable struct from parent to child. So I am now passing a callback with IN of ComponentLink<Child> to child in props. Then in child's create I pass a clone of its link to parent. Just wanted to share.

muhuk avatar Jul 05 '20 17:07 muhuk

https://github.com/yewstack/yew/pull/2321 adds a section about component communication (see here). It suggests using contexts to communicate but there is no explanation on how to actually do it. Context documentation mostly consists of two giant code blocks. This needs improvement.

Related: https://github.com/yewstack/yew/issues/2189

ranile avatar Jan 01 '22 17:01 ranile

Since this is the first thing that comes up in google I thought I might aswell post my solution here: https://dev.to/davidcks/how-to-send-a-message-to-a-child-component-in-yew-503

Basically I have a link field in my parent component which gets updated when a certain Message is received. I then send a message to the parent from within the child containing its Scope so it can update the parents field with that childs Scope.

Now the Scope is available in the parent and messages can be dispatched.

DavidCks avatar Nov 04 '22 09:11 DavidCks