如何使用ReactJS添加或删除类

132 阅读1分钟
   const ref = useRef(null)
  const handleClick = () => {
  if(ref.current.classList.contains('hidden')){
    ref.current.classList.remove('hidden')
  }else{
    ref.current.classList.add('hidden')
  }
 }
   

未发现的类型错误。无法读取null的属性(读取'classList'):

             <div className='px-4 cursor-pointer md:hidden' id='burger' onClick={handleClick()}>
          <svg xmlns="http://www.w3.org/2000/svg" className="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
            <path strokeLinecap="round" strokeLinejoin="round" d="M4 6h16M4 12h16M4 18h16" />
          </svg>
          </div>
          <ul className='text-sm mt-6 hidden md:block' id='menu' ref={ref}> 

我在ReactJS中使用tailwindcss