javascript - onPointerOver and onPointerOut detect all child elements - Stack Overflow
The problem is that I have onPointerOver
and onPointerOut
triggered on each child element, but I need it to be on a common group, because then the component is re-rendered too often.
There is a group here, and now when I move the mouse cursor, the component is redrawn many times, I want my state to change only 1 time when I move the mouse cursor and move it away.
This is changing, but the fact is that my head consists of many InstancedMesh
, and it turns out that this state changes for each square that my head consists of.
How do I make this work only for the entire group, that is, so that when I hover the cursor, it only reacts to the entire group, and not to individual squares?
Sandbox
<group
position={position}
onPointerOver={(e) => {
e.stopPropagation();
handlePointerOver(e);
}}
onPointerOut={(e) => {
e.stopPropagation();
handlePointerOut(e);
}}
>
<Head scale={scale} isVisibleGrid={isVisibleGrid} position={scaledPosition(0, 24, 0)} />
</group>
const handlePointerOver = (e: ThreeEvent<PointerEvent>) => {
setIsVisibleGrid(true);
e.stopPropagation();
};
const handlePointerOut = (e: ThreeEvent<PointerEvent>) => {
setIsVisibleGrid(false);
e.stopPropagation();
};
The problem is that I have onPointerOver
and onPointerOut
triggered on each child element, but I need it to be on a common group, because then the component is re-rendered too often.
There is a group here, and now when I move the mouse cursor, the component is redrawn many times, I want my state to change only 1 time when I move the mouse cursor and move it away.
This is changing, but the fact is that my head consists of many InstancedMesh
, and it turns out that this state changes for each square that my head consists of.
How do I make this work only for the entire group, that is, so that when I hover the cursor, it only reacts to the entire group, and not to individual squares?
Sandbox
<group
position={position}
onPointerOver={(e) => {
e.stopPropagation();
handlePointerOver(e);
}}
onPointerOut={(e) => {
e.stopPropagation();
handlePointerOut(e);
}}
>
<Head scale={scale} isVisibleGrid={isVisibleGrid} position={scaledPosition(0, 24, 0)} />
</group>
const handlePointerOver = (e: ThreeEvent<PointerEvent>) => {
setIsVisibleGrid(true);
e.stopPropagation();
};
const handlePointerOut = (e: ThreeEvent<PointerEvent>) => {
setIsVisibleGrid(false);
e.stopPropagation();
};
Share
Improve this question
edited 6 hours ago
Vadim Semashko
asked yesterday
Vadim SemashkoVadim Semashko
211 silver badge2 bronze badges
New contributor
Vadim Semashko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
- 2 Any chance for Sandbox, because is quite wide topic? – Łukasz Daniel Mastalerz Commented yesterday
1 Answer
Reset to default 0Capture phase events may be useful here
A sample code below shows the same. The code uses click event for demo purpose.
App.js
export default function App() {
return (
<>
<div
onClickCapture={(e) => {
console.log('Parent captured');
e.stopPropagation();
}}
>
Parent
<br />
<Child />
</div>
</>
);
}
function Child() {
return <div onClick={(e) => console.log('Child listened')}>Child</div>;
}
Test run
On load of the App
On clicking on the text Child
Console log generated as below:
// Parent captured
Observation:
Though the click event has been handled in the child component, this handler did not fire when clicking on it. This is due to the capture click event in Parent. It captures the click. Additionally parent stops its propagation too. Since the event propagation has been stopped in Parent, it does not propagate to Child.
- 能跑安卓的锐龙3000C原来是它!热设计功耗低至4.5W
- 大本营不保?美国安卓系统激活量超iOS
- Google的AR眼镜
- regex - How do I force nginx basic authentication for files starting with a certain mask? - Stack Overflow
- excel - Send email using new Outlook - Stack Overflow
- frontend - Rescript and Fetch with Post Request - Stack Overflow
- Banning telegram channels from groups as a bot - Stack Overflow
- Laravel Livewire Pagination is not responsive - Stack Overflow
- wpf - Installation of an upgrade version starting at ResumeDlg - Stack Overflow
- vue.js - single pages doesn't work when i moved them to app component - Stack Overflow
- i tried to get read edit manage storage in android java and output permission denied and not found in device setting - Stack Ove
- solrcloud - How to use "or" in an eDisMax query in Solr 9.4? - Stack Overflow
- react native - How to convert location of each corner relative to the Camera Preview to the device view? - Stack Overflow
- python - overrideredirect() no longer working on MAC? - Stack Overflow
- python - ReCaptcha v3 works locally but not in deployment - Stack Overflow
- How to Use GraalVM to Execute JavaScript Code Making REST Calls Using Java? - Stack Overflow
- swift - Popovers not displayed inside ForEach over Enum types - Stack Overflow