1

I'm rendering items (custom lwc component) in a for each template:

<template if:true={items.data}>
        <template for:each={items.data} for:item="item">
            <c-lwc-item
                key={item.id}
                sf-id={item.id}
                onsf_item_loaded={handleLoad}>
            </c-lwc-item>
        </template>
</template>

The 'sf_item_loaded' is a custom event dispatched by that component.

I'd like, in the handleLoad method triggered in the onsf_item_loaded, to access the c-lwc-item that dispatched that event. Is that possible? I want to do it so that I can call a method from that component.

1 Answer 1

1

You can get that component using the .target property of the event.

handleLoad(event) {
    let target = event.target;
    console.log('target ' + target);
    // you can access the function or any property using that like
    target.someFunction();
}
6
  • Can't he use the .target property of the event? Commented Dec 29, 2020 at 13:03
  • Good catch! I did not try that. I will give it a try. Commented Dec 29, 2020 at 13:04
  • 1
    @RenatoOliveira yes we can use the event.target, thanks!! Commented Dec 29, 2020 at 13:10
  • So, the event.target returns the component in question? I can let the detail return what it did originally and use the target directly? Commented Dec 29, 2020 at 13:13
  • 1
    Thank you very much! Commented Dec 29, 2020 at 13:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .