`setState` in React portal containing a text input causes browser to scroll in Safari
`setState` in React portal containing a text input causes browser to scroll in Safari
If you place a text input inside a React portal overlay and the content adjacent to it scrolls, any event that changes state causes Safari to scroll to the portal (which is the bottom of the page).
To recreate
setState
setState
Only happens in Safari (currently using 11.1.2)
import * as React from 'react';
import createPortal from 'react-dom';
export default class App extends React.Component
const portalActive, randomBoolean = this.state;
togglePortal = () =>
this.setState((portalActive) =>
return portalActive: !portalActive;
);
;
toggleBoolean = () =>
this.setState((randomBoolean) =>
return randomBoolean: !randomBoolean;
);
;
render()
const textInput = ;
const portal = this.state.portalActive
? (textInput)
: null;
return (
<>
<button onClick=this.togglePortal
portal
Array.from(length: 40).map(() => (
<p>Content that causes scrolling</p>
))
</>
);
class Portal extends React.Component
const children, idPrefix = this.props;
componentWillMount()
const portalNode = document.createElement('div');
document.body.appendChild(portalNode);
componentWillUnmount()
document.body.removeChild(portalNode);
render()
return createPortal(children, portalNode);
Thanks, Ricardo. Yes, I was trying to avoid a hacky solution, but it might be my only choice.
– Solona Armstrong
Sep 10 '18 at 19:39
0
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
window.scrollTo(0, 0); this does not necessarily fix your problem but might be a work around until you find a solution. This scrolls to the top of the page
– Ricardo Costa
Sep 10 '18 at 15:17