Body class not removed under certain circumstances
See original GitHub issueSummary:
If React Modal is mounted and almost immediately unmounted, the body’s class is not removed when the modal disappears. If this class is used to prevent scrolling, the body will remain unscrollable forever.
Steps to reproduce:
- Create a project using React Modal.
- Add the following component somewhere on the page:
import React, { useEffect, useState } from 'react';
import Modal from 'react-modal';
export const Flash = () => {
const [hide, setHide] = useState(0);
useEffect(() => {
if (!hide) {
setHide(true);
}
}, [hide])
return <>
{!hide && <Modal isOpen={true}>
<div>
Modal text.
</div>
</Modal>}
Other text
</>
}
- Load the page and inspect the body element. Note that it still has the class
ReactModal__Body--open
even though no modal is open.
Expected behavior:
The body element should not have the class ReactModal__Body--open
.
Link to example of issue:
https://codesandbox.io/s/react-modal-unmount-bug-duvib?file=/src/App.js
Additional notes:
This is happens because the example component is unmounting the modal immediately in a useEffect. This causes it to unmount after componentDidMount
is called, but before ModalPortal’s setState
takes effect. So when componentWillUnmount
is called on ModalPortal, it does not yet have the open state, so it does not decrement the counter of the className.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:6
- Comments:11
Top Results From Across the Web
Javascript, fastest way to remove a class from `<body>`
And I want to remove the no-javascript class from it, after it's being read by the browser. <body class="foo boo no-javascript bla"> <script...
Read more >Remove classes from body_class - WordPress Stack Exchange
In the case I built this for I needed to restrict the body classes to a specific set globally and then remove others...
Read more >Adding to the Body Class in WordPress - Web Design
Specifically, we cover adding body classes, removing body classes, conditionally adding body classes, and some use cases.
Read more >Ovarian Cancer: Symptoms, Diagnosis & Treatment
The goal in treating cancer is to remove as much, if not all, cancer from your body as possible. Common ovarian cancer treatments...
Read more >.removeClass() | jQuery API Documentation
More than one class may be removed at a time, separated by a space, ... the className property of the selected elements, not...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Maybe the actions in
beforeOpen
can be called after theisOpen
state is set to true, instead of before. That way it won’t add the class until the state is actually set. It at least does the trick at first glance.I have no clue if that would break something else, but I wanted to offer something instead of just reporting this and dipping!
@vadymshymko it’s ok as a workaround for the problem, but it should not be handled by the user.
We need to investigate this further. Although, I don’t have time to work on this, I can assist if you guys want to give it a try. Just @ me an I’ll answer.