Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用react在两个css类之间切换_Javascript_Css_Reactjs - Fatal编程技术网

Javascript 如何使用react在两个css类之间切换

Javascript 如何使用react在两个css类之间切换,javascript,css,reactjs,Javascript,Css,Reactjs,我正在尝试在两个类黑暗模式和正常模式之间切换 这是香草js eventlistener 单击此按钮可在两种模式之间切换。如何使用react实现这一点 const[active,setActive]=useState(false) const handleToggle=()=>{ setActive(!active) } 返回( ) 在这种情况下,您可以使用useRef: const [active, setActive] = useState(false) const modeRef = use

我正在尝试在两个类黑暗模式和正常模式之间切换

这是香草js eventlistener

单击此按钮可在两种模式之间切换。如何使用react实现这一点

const[active,setActive]=useState(false)
const handleToggle=()=>{
setActive(!active)
}
返回(
)

在这种情况下,您可以使用
useRef

const [active, setActive] = useState(false)
const modeRef = useRef();

const handleToggle = () => {
     modeRef.current.classList.toggle("dark")
}

return (
    <button ref={modeRef} className="mode-switch" title="Switch Theme" onClick={handleToggle}>
          <svg className="moon" fill="none" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" width="24" height="24" viewBox="0 0 24 24">
            <defs></defs>
            <path d="M21 12.79A9 9 0 1111.21 3 7 7 0 0021 12.79z"></path>
          </svg>
    </button>
)
const[active,setActive]=useState(false)
const modeRef=useRef();
const handleToggle=()=>{
modeRef.current.classList.toggle(“暗”)
}
返回(
)
     const [active, setActive] = useState(false)

     const handleToggle = () => {
      setActive(!active)
     }

 return (
    <button className="mode-switch" title="Switch Theme" onClick={handleToggle}>
          <svg className="moon" fill="none" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" width="24" height="24" viewBox="0 0 24 24">
            <defs></defs>
            <path d="M21 12.79A9 9 0 1111.21 3 7 7 0 0021 12.79z"></path>
          </svg>
        </button>
)
const [active, setActive] = useState(false)
const modeRef = useRef();

const handleToggle = () => {
     modeRef.current.classList.toggle("dark")
}

return (
    <button ref={modeRef} className="mode-switch" title="Switch Theme" onClick={handleToggle}>
          <svg className="moon" fill="none" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" width="24" height="24" viewBox="0 0 24 24">
            <defs></defs>
            <path d="M21 12.79A9 9 0 1111.21 3 7 7 0 0021 12.79z"></path>
          </svg>
    </button>
)