I want to reload my ReactJS Page without refreshing it! There will be 'reload icon' in the page if some person click it then it should not Reload the page it should be single Page only!
Don't tell window.location.reload();
consider conditionally early returning the Redirect component : https://reactrouter.com/web/api/Redirect
so on your button click; setReload(true);
in your page component:
// before your regular return
if(reload) return <Redirect to={window.location.pathname} />;
If your component is a class component you can use this code below in the click function. this.forceUpdate().
if your component is a functional one you can follow this answer
Related
Instagram example
As in the image above, in the user profile page with a url(profile/:username), the user is able to click on one of his posts which opens a modal containing that specific post, the url thereafter changes to p/:postId but doesn't navigate to another page, it instead displays above the profile page without navigating to p/:postId
I'm trying to do the same in my app and I have tried doing stuff like history.push but it's not working and I assume this is happening cause of react-router.
To achieve the behavior you described, you can use React Router to manage the URL change and display the modal with the post content.
Define a route in your React Router configuration that corresponds to the modal content you want to display. For example
<Route path="/posts/:postId" component={PostModal} />
In your component, include a link to the new route with the post ID as a parameter.
<Link to={`/posts/${postId}`}>View Post</Link>
In your PostModal component, use the useParams hook to access the post ID parameter from the URL. Then, fetch the post data from your API or store, and display it.
import { useParams } from 'react-router-dom';
function PostModal() {
const { postId } = useParams();
// Fetch post data and display it in modal
}
After that, Use CSS to display the modal as an overlay on top of your existing content, and use JavaScript to show and hide the modal as appropriate.
In React router, can I use history.push to change the page, and also go to a specific section/anchor on the new page? For example:
history.push('/home#section1');
In my home page I have this:
<div class='back backCard backCardFour ' id='section1'>
<div>
Then please don't hesitate to contact us, you can
use "Send us your thoughts that it's located on the
top of the page and ask us everything or give some
suggestions to improve our work
</div>
</div>
React Router itself doesn't support anchors.
You can use something like React Router Hash Link for that.
You can redirect with props, eg.:
history.push('/home', 'section1');
And then inside your '/home' component check for these 'redirect' props. If there is one, call a scrollIntoView function:
componentDidMount() {
const view = this.props.location.state;
const section1 = document.getElementById('section1');
if (view) {
section1.scrollIntoView();
}
}
I have a use case where I need to navigate from one salesforce tab to other. I want the tab to which I navigate to reload as I want it to render and per new data which I will be setting in the previous tab.
I have tried using following code but it does not seem to reload the page. It just navigates to the tab.
this[NavigationMixin.Navigate]({
type: 'standard__navItemPage',
attributes: {
apiName: 'tab_api_name'
},
});
If not complete reload, is there any method which we can invoke on switching/navigating between salesforce tabs as it did not call any of the lifecycle methods while switching tabs.
You can try window.open(url, "_self");
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'm using react-router-dom of version 4.2.2.
In my application, I have two pages.
I'm using Link to redirect and I'm passing state while coming from 2nd page to 1st page to show notification.
<Link
to={{
pathname: '/page1',
state: { success: true }
}}>
Based on success state, I decide to show notification.
After coming back to page1, if I refresh that page, I keep seeing notification because react-router state is not cleared. I just want to show notification when I come back to page1 from page2. So how do I clear react-router state without redirecting to the same path?
How do i solve this problem?