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

后端 未结 5 576
广开言路
广开言路 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: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 component

提交回复
热议问题