question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Body class not removed under certain circumstances

See original GitHub issue

Summary:

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:

  1. Create a project using React Modal.
  2. 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
   </>
}
  1. 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:open
  • Created 2 years ago
  • Reactions:6
  • Comments:11

github_iconTop GitHub Comments

1reaction
Plygoncommented, Jun 15, 2021

Maybe the actions in beforeOpen can be called after the isOpen 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!

0reactions
diasbrunocommented, Jun 21, 2022

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found