Add a class to the HTML <body> tag with React?

后端 未结 5 575
广开言路
广开言路 2021-01-30 16:57

Im making a modal in my React project that requires a class to be added to the body when the modal is open and removed when it is closed.

I could do this the old jQuery

相关标签:
5条回答
  • 2021-01-30 17:08

    Actually you don't need 2 functions for opening and closing, you could use document.body.classList.toggle

    const [isOpen, setIsOpen] = useState(false)
    useEffect(() => {
      document.body.classList.toggle('modal-open', isMobileOpen);
    },[isOpen])
        
    <button onCLick={()=> setIsOpen(!isOpen)}>Toggle Modal</button>
    
    0 讨论(0)
  • 2021-01-30 17:12

    Like what @brian mentioned, try having a top-level container component that wraps around your other components. (assuming you're not using redux in your app)

    In this top-level component:

    1. Add a boolean state (eg. modalOpen) to toggle the CSS class
    2. Add methods (eg. handleOpenModal & handleCloseModal) to modify the boolean state.
    3. Pass the methods created above as props into your <Modal /> component
    0 讨论(0)
  • 2021-01-30 17:17

    TL;DR use document.body.classList.add and document.body.classList.remove

    I would have two functions that toggle a piece of state to show/hide the modal within your outer component.

    Inside these functions I would use the document.body.classList.add and document.body.classList.remove methods to manipulate the body class dependant on the modal's state like below:

    openModal = (event) => {
      document.body.classList.add('modal-open');
      this.setState({ showModal: true });
    }
    hideModal = (event) => {
      document.body.classList.remove('modal-open');
      this.setState({ showModal: false });
    }
    
    0 讨论(0)
  • 2021-01-30 17:26

    With the new React (16.8) this can be solved with hooks:

    import {useEffect} from 'react';
    
    const addBodyClass = className => document.body.classList.add(className);
    const removeBodyClass = className => document.body.classList.remove(className);
    
    export default function useBodyClass(className) {
        useEffect(
            () => {
                // Set up
                className instanceof Array ? className.map(addBodyClass) : addBodyClass(className);
    
                // Clean up
                return () => {
                    className instanceof Array
                        ? className.map(removeBodyClass)
                        : removeBodyClass(className);
                };
            },
            [className]
        );
    }
    

    then, in the component

    export const Sidebar = ({position = 'left', children}) => {
        useBodyClass(`page--sidebar-${position}`);
        return (
            <aside className="...">
                {children}
            </aside>
        );
    };
    
    0 讨论(0)
  • 2021-01-30 17:29

    ReactJS has an official React Modal component, I would just use that: https://github.com/reactjs/react-modal

    0 讨论(0)
提交回复
热议问题