admin管理员组文章数量:1406436
I have a parent component with two event functions handleMouseEnter and handleMouseLeave. The child component receives these two functions via props, so my goal is to make some delay in the execution of these functions. Some "GanttDiagramAction" have the same order_id, and if so, after mouseEnter I don't need to re-render the child components.
When i use this func like this:
const [currentOrderId, setCurOrderId] = useState()
const handleMouseEnter = orderId => {
setCurOrderId(orderId)
}
const handleMouseLeave = () => {setCurOrderId(null)}
//over map
<Box>
{data.map(item => (
<GanttDiagramAction
onMouseEnter={() => handleMouseEnter(order_id)}
onMouseLeave={()=> handleMouseLeave()}
/>
)}
</Box>
Everything fine and child components not rerendered if "order_id" the same. But i need delay, and when i tried to use debounce, child component rerendered twice, even if order_id the same.
I have a parent component with two event functions handleMouseEnter and handleMouseLeave. The child component receives these two functions via props, so my goal is to make some delay in the execution of these functions. Some "GanttDiagramAction" have the same order_id, and if so, after mouseEnter I don't need to re-render the child components.
When i use this func like this:
const [currentOrderId, setCurOrderId] = useState()
const handleMouseEnter = orderId => {
setCurOrderId(orderId)
}
const handleMouseLeave = () => {setCurOrderId(null)}
//over map
<Box>
{data.map(item => (
<GanttDiagramAction
onMouseEnter={() => handleMouseEnter(order_id)}
onMouseLeave={()=> handleMouseLeave()}
/>
)}
</Box>
Everything fine and child components not rerendered if "order_id" the same. But i need delay, and when i tried to use debounce, child component rerendered twice, even if order_id the same.
Share Improve this question asked Mar 5 at 20:09 David AbramovDavid Abramov 537 bronze badges1 Answer
Reset to default 0So this solution is work for me:
// Debounced handleMouseEnter with a delay of 500ms
const debouncedHandleMouseEnter = useDebouncedCallback(orderId => {
setCurOrderId(orderId)
}, 500)
// Debounced handleMouseLeave with a delay of 500ms
const debouncedHandleMouseLeave = useDebouncedCallback(() => {
setCurOrderId(null)
}, 500)
// Memoize the functions to avoid recreating them on every render
const handleMouseEnter = useCallback(
orderId => {
debouncedHandleMouseEnter(orderId)
},
[debouncedHandleMouseEnter]
)
const handleMouseLeave = useCallback(() => {
debouncedHandleMouseLeave()
}, [debouncedHandleMouseLeave])
本文标签: reactjsMake debouncing function on eventsStack Overflow
版权声明:本文标题:reactjs - Make debouncing function on events - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745010049a2637495.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论