can i propagate event to child?
is there a mechanism to propagate click event from parent to child?
or is there any alternative? for example by returning component and a signal
Accessor
.
but todo so, i need factory function. i want to avoid the need for factory function.15 Replies
i can just export a global variable. but doing so prevent me create multiple instance of the component.
or maybe my component could accept a callback? is that better?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
normally you don't pass whole events to children, and just pass some state via props but if you need to the solution is the same as with data, pass a signal containing the event via a prop to the child
So I have a list component (
ul
) what accept JSX.ArrayComponent
as children.
i want to be able to put anything to it as list item, like anchor element a
or anything with onClick
event attached to it.
the problem is that the item container (li
) is bigger than its child component. i want to be able to click on the container (not exactly at the text) and run an action.
the item inside the li
could be anything, from simple text to svg icon. each item may have different action.
my current approach is to accept a callback that look like this.
the action dispatch/routing would be handled by the parent component.You can just use a signal to capture the event and use it inside an effect in the child.
It should be noted that the reactive system will get the event out of the event cycle, so things that require authentic events (eg starting video) will not work.
i have the same thought, to use effect. but idk how to do so. because i dont have control over the children.
how can i pass the
accessor
or click
, to the children?yes.
using props
oh, so i pass the accessor to the children and pass setter to the list?
yes.
if you need to react on the actual event, you can also turn the pattern on its head: give a setter to the client that allows to set the event handler.
idk how to do it with this method.
What do you mean, you don't have control over the children? What exactly are you trying to achieve?
idk what to pass to List and each Child
Ah, ok. Here's a nice pattern in solid to help you achieving that. You can use an array event handler union.
There are two ways to do the parent-to-child communication; 1. is to use a FunctionComponent: make sure that el is
(props) => JSX.Element
and not an already rendered element very much like the one Child of <For>. 2. is context.
and i'd use
List
component like this?
thanks for your help 👍, that's the last question for today.
i'll keep the thread open to see feedback from other members.Unknown User•2y ago
Message Not Public
Sign In & Join Server To View