A
A
ArturMavlidov2022-03-26 23:55:55
React
ArturMavlidov, 2022-03-26 23:55:55

Is it possible to hang an event handler on the document in a frequently repeated component?

Imagine that we have 30 such components on the page, and in each handler for the document (click)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin B., 2022-03-27
@ArturMavlidov

It all depends on the context of what you are doing. If, for example, this is the same handler, then it is better to move it to the parent. But if, for example, this is some kind of logic that is used inside only each child component separately (isolated from others), then it cannot be taken out in any way. Therefore, the question needs to be supplemented with data about what kind of handler and on what event it is triggered

E
Egor Zhivagin, 2022-03-27
@Krasnodar_etc

So that there are 30 identical handlers on the document? What's the point?
Can it be enough to hang one handler from the top-level component? Delegation, all things
The answer to your question - you can, of course, but it's better to look for a more optimized solution

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question