Well I have a very simple website, the body of the site should scroll such that the address bar is no longer visible. from this topic it seems that simply calling the follwing ought to do the trick:
window.scrollTo(0,1)
However in my react app this is not working at all, the application is:
import * as React from 'react';
import {Route, Router} from 'react-router';
class App extends React.Component<PropTy> {
componentDidMount() {
console.log('ok');
window.scrollTo(0, 1);
}
render() {
return (<div style={{height: '100vh', position: 'relative'}}>
text
</div>);
}
}
export default App;
I can manually scroll down (on mobile). However it doesn't do this automatically - yet I do see the "ok" in the console.
The index.html and manifest.json have hardly changed from the default create-react-app:
<!DOCTYPE html>
<!--suppress HtmlUnknownTarget -->
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#7B0000">
<meta name="Description" content="Spots platform, event calendar, enroll">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<title>AllSports</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root" ></div>
</body>
</html>
What causes the browser to just ignore scrollTo()
- I've tried different offsets for scrolling but none of them seemed to work?
I've also tried modifying the index.html:
<!DOCTYPE html>
<!--suppress HtmlUnknownTarget -->
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#7B0000">
<meta name="Description" content="Spots platform, event calendar, enroll">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<title>AllSports</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root" style="height: 100vh; position: relative;"></div>
</body>
</html>
But that doesn't seem to work either.
I now notice (thanks for the comment) that this is due to react router import. If I remove the import it "works'. - The application is however tightly coupled with react-router, so how would I "fix" this?