Skip to content

Instantly share code, notes, and snippets.

@alex-boom
Last active November 12, 2020 15:13
Show Gist options
  • Save alex-boom/c8b4612a4cc564cc010ef0ed47f77fad to your computer and use it in GitHub Desktop.
Save alex-boom/c8b4612a4cc564cc010ef0ed47f77fad to your computer and use it in GitHub Desktop.
react toggle class
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