working on single page appliction trying use popstate to navigate back to previous page but I have to click twice to take me back. I want to make it happen for one click. console showing for each click but needs twice click to navigate back. using react native.
also using props.navigator.push for single page application
useEffect(() => {
window.addEventListener('popstate', (event) => { console.log( event) });
}, []);
I am trying to implement a way where whenever I change a page, it scrolls to the top. So far, I have tried adding this:
useEffect(() => {
window.scrollTo(0, 0);
}, []);
But this does not help. Is there anyway to make it so whenever a new link is opened through react-router, the app automatically goes to the top? I am using component based functions and would prefer a solution that uses hooks.
I have a webapp that shows some content on say,
test.com and special.test.com
I have setup PWA using React Typescript + PWA template.
The "Add to Home Screen" appears on both the urls.
Is their a way to prevent the install option from appearing in special.test.com
self.addEventListener('beforeinstallprompt', (event) => {
const subdomainName = self.location.host.split('.')[0];
console.log('PWA_INSTALL_CALL', subdomainName);
if (subdomainName === 'special') {
event.preventDefault();
}
});
I have added this event listener to service-worker.ts but this function doesn't seem to be called since I'm not seeing console.log statement on Chrome dev console.
Really stuck here!
Please help. Thanks!
You need to listen for beforeinstallprompt in the application context, not in a service worker context.
I'm using react-router-dom for routing in my reactjs app. And i want to prevent user from going back after login i.e i don't want to user go back again on login screen when he hit back button on browser after login.
Using componentDidUpdate method of React page lifecycle, you can handled or disabled go back functionality in browser. basically componentDidUpdate method will call automatocally when component got updated. so once your component is updated you can prevent to go back as below.
componentDidUpdate() {
window.history.pushState(null, document.title, window.location.href);
window.addEventListener('popstate', function(event) {
window.history.pushState(null, document.title, window.location.href);
});
}
This will prevent to user go back and uses the current page as a refernce for history object . so incase of user even click on back button of browser, they can not go backword in the last page.
You can checkout this codesandbox example may be that will help you or someone who is looking for the same.In this we can prevent the user to go back to previous page,For more detail checkout this medium article. here is small part of the code
componentDidMount() {
const { history } = this.props;
window.addEventListener("popstate", () => {
history.go(1);
});
}
for full working example click on this link.
Are you using redux? other wise you can add something on your render that checks if the user is already logged in it redirects him back to the page he was like:
import { Redirect } from 'react-router'
render(){
//I store my user JWT on this.props.currentUser redux state
this.props.currentUser && <Redirect to={'whatever page you want'} />
//OR you can also, if you have history, history.goBack()
So instead of forbidding going back, you forbid the user to ever going to the login page while logged in, it redirects him somewhere or back to where he was
window.addEventListener('popstate', function(event) {
});
use this function this will read your browser back button click event and after that whatever condition you want you can apply
I faced with interesting problem in React JS. I'm opening main page of my application with the list of products. Then I chose few products and open them in separate tabs (browser windows). When I'm starting to work with each tab my localStorage changes (product is added to cart) but only on the current page which I'm working on. Is there any possible ways to update (synchronize) localStorage for all opened tabs (browser windows) in React JS (Redux), not only on current page?
You want to add an event listener using window.addEventListener('storage', function() {}) and update your application state either using setState or dispatching an action with redux
componentDidMount() {
window.addEventListener('storage', this.handleStorageChange);
}
componentWillUnmount() {
window.removeEventListener('storage', this.handleStorageChange)
}
handleStorageChange(storageEvt) {
// setState or dispatch a redux action
this.setState({
});
}