Last active
November 12, 2020 15:13
-
-
Save alex-boom/c8b4612a4cc564cc010ef0ed47f77fad to your computer and use it in GitHub Desktop.
react toggle class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from "react"; | |
//Способ 1 | |
const Example = () => { | |
const [ isActive, setActive ] = useState(false); | |
const handleToggle = (e) => { | |
e.preventDefault(); | |
setActive(!isActive); | |
}; | |
return ( | |
<div className={ `main-layoyut ${ isActive ? "hide-sidebar" : "" }` }> | |
<a onClick={ handleToggle }>Toggle button</a> | |
</div> | |
); | |
} | |
export default Example; | |
//Способ 2 | |
const Example = () => { | |
const handleToggle = (e) => { | |
e.preventDefault(); | |
document.querySelector('.main-layoyut').classList.toggle('active'); | |
}; | |
return ( | |
<div className="main-layoyut"> | |
<a onClick={ handleToggle }>Toggle button</a> | |
</div> | |
); | |
} | |
export default Example; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment