`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);



enter image description here



enter image description here






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






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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)