let inner=useRef();
let inner_exit=useRef();
useEffect(()=>{
inner.current=document.querySelectorAll('.inner');
inner_exit.current=document.querySelectorAll('.exit');
inner_exit.current?.[0].addEventListener("click",toogleInner);
inner.current?.[0].addEventListener("click",(e)=>{
if(e.target === e.currentTarget) toogleInner();
});
},)
const toogleInner = ()=>{
inner.current?.[0].classList.toggle('hide');
};
return (
<div className='inner'>
<div className='contentSign'>
<div className='form'>
<h2>Đăng nhập</h2>
<span className='exit'>X</span>
</div>
</div>
)
};
I want when i click on space or tag span "X" tag div class "inner" to be hide, but when I click it, it re-renders twice. I find many ways, like: useCallBack
, use Memo, but I don’t know how to use them for my code.Can you fix it for me, please?
I don’t know how to fix this bug
2
Answers
It looks like there are a few issues in your code. Here’s a revised version using
useCallback
anduseEffect
to handle the event listeners more efficiently:This version uses
useCallback
to memoize thetoggleInner
function and ensures proper cleanup of event listeners using the cleanup function returned byuseEffect
.This is poor react. It is not common to use direct DOM APIs to add event listeners in react (it’s not forbidden, just not the norm).
Also, the "number of renders" is almost never something you should ever be concerned about. You should also never try to optimize performance if you don’t have a performance problem (or imminently foresee one)
Trying to avoid the render defeats the entire purpose of using react for it’s ability to provide simplicity and rapid development.
Writing code like you have above will not improve your React skills.