pradel / react-responsive-modal

Simple responsive react modal
https://react-responsive-modal.leopradel.com/
MIT License
606 stars 95 forks source link

Internal state does not update, even though property `open` changed #419

Closed alexbepple closed 3 years ago

alexbepple commented 4 years ago

I work in a closed environment. Last weekend Chrome was updated for some users from 76 to 83. This caused our modals to break: they would remain open when they should not. We have not been able to find the root cause. But we have traced this to the following behavior: we change the property open programmatically, but the internal state sometimes (!) does not update.

From the React Developer Tools: rrm prop false state true

This happens with both React 16.8.6 and 16.13.1, and also with Chrome 84, on Windows.

alexbepple commented 4 years ago

We are trying to create a somewhat reproducible example on CodePen or similar, but to no avail so far.

We switched to react-modal as a quick fix. It also uses a boolean property. We did not have to change any of our logic. This supports our hypothesis that this is a bug in react-responsive-modal.

I am by no means sure, but I suspect that

const [showPortal, setShowPortal] = useState(open);

(https://github.com/pradel/react-responsive-modal/blob/master/src/index.tsx#L169) might be a problem.

I think that useState(prop) does not update state correctly when prop changes. Maybe this would help?

const [showPortal, setShowPortal] = React.useState(false);
React.useEffect(() => { setShowPortal(open); }, [showPortal]);
memon07 commented 4 years ago

@alexbepple yes , i am facing the same issue .

pradel commented 3 years ago

@alexbepple Thanks for reporting the issue, should be fixed in v5.2.1 :)