Lost the function in history.push after reload - reactjs

history.push({
pathname:
vaccineTaken == "Yes" ||
(vaccineTaken == 1 ? "/vaccination" : "/no-vaccine"),
state: data,
onCancel: getExistingSurvey, // This is function
});
the attribute get cleared after reloading the tab, i have tried to do
state:{ onCancel: getExistingSurvey}
but it gives an error

this is happening because when you reload, you are directly calling the route. which in turn will not have the history object from before the reload.
it is a bad practice to put state in history. use Proper State management like redux or react hooks.

Related

Next.js - Is it possible to detect when a page is accessed directly or via <Link> or router.push.?

I want to implement some conditional scrollTo behavior depending whether the page is accessed directly by typing the URL into browser VS when the page is rendered as a result of a route navigation via <Link> or router.push()
Is there a way to distinguish between the two in Next.js?
EDIT 1:
Thanks to some hints in the comments i've found one way but not sure if its any reliable:
useEffect(() => {
if (Object.keys(router.components).length == 2) {
console.log("Is fullpage refresh");
} else {
console.log("Is is route change");
}
}, []);

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

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!

On refreshing page, react router state shows old state

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?

React Router: this.props.location.state is null

I am using react-router 2.4.0 and I am trying to pass in some state to my route on a click event. It passes in pathname and query correctly but state is null.
Below is my handler function
clickHandler: function() {
var navigate = { pathname: '/view/report',
query: { filter: "filter-val"},
state: {"field": this.state.field}
}
this.props.router.push(navigate);
In my component that renders the route, inside getInitialState I am trying to access the state but it comes up as null.
I logged the this.state.field and it is present after the button press but before routing. I also logged this.props.location inside thegetInitialState of the routed component and it has non-null values for things like query, pathname, etc. but state (this.props.location.state to be specific) shows up as null.
Is there something special I need to do to set an object inside state of the router's location?
v2.4.0 is an old version. While I'm not aware of any reason why this would be causing your error, it might help to try upgrade to v3 (just v2 with v1's deprecated code removed).

Resources