On refreshing page, react router state shows old state - reactjs

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?

Related

Changing the route when opening a modal but not navigating to it

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.

How to Reload in ReactJS without refreshing the page?

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

How React Router persist state object?

I need to know how react-router-dom persists state object, for example i have a route like this: 'http://localhost:3000/somepath/123', if i open this route on the new browser tab, state object is obviously undefined, if i redirected from somewhere to this route then state is an object from application state, this is completely normal... but the thing is, when i redirected from different route and i refresh the page several times the state is again an object, how react persist state even if i refresh the page?
<Link
to={{
pathname: '/somepath/:someid',
state: state.obj[someid]
}}
>
Link to somepath
</Link>
React-router-dom doesn't persist state. Browser does.
React router uses history API to push & pop states.
The behaviour you describe is how history.state works.
So after you refresh, browser keeps the history state.
Check History API

How to prevent user from going back in react-router-dom?

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

ReactJs: pass param in <Link> tag and open next page in new tab (Not working)

I am trying to send params through state to <Link> tag and trying to access that params in next component which will be opened in new tab. But while accessing params it says undefined.
The way I call Post detail screen and pass params through state:
<Link
to={{
pathname: `/post/${props.post.slug}`,
state: {post: props.post}
}}></Link>
The way I am accessing params in PostDetail.js component.
const post = this.props.location.state.post;
It says post is undefined. So how can I achieve this?
If I try to this in single page flow, it will work(even if you refresh page). But when I try to open post detail in new tab, it gives error!
Note: ignore pathname and its value in above example!

Resources