React router v6.0.0-beta.1 go back / pop history - react-router

I am trying to implement "go back" functionality for a page that could be access from n different urls.
Example, all the following URLs will link to abc.com/b1:
abc.com/a1
abc.com/a2
abc.com/a3
abc.com/a4
...
abc.com/an
From abc.com/b1 I want to support a link that takes you back to the previous page you visited.
My first attempt is to use:
...
const navigate = useNavigate();
navigate(-1);
...
However, if you land straight in abc.com/b1 navigate(-1) will likely take you outside of my application into some other site, or browser about:blank.
What is the best approach to implement such functionality?
is there a way to access the history length to check wether we should navigate(-1) or push a new page?
should I pass additional state/query from a pages to b? so b knows how to go back? this seems like overkill.
Thanks.

I was with the same problem, but I finally found a solution that worked for me.
I'm using the react-router-dom#6.2.1.
// Simply return to the previous page or to the initial route
navigate('..')

Related

React history - is there a way to remove duplicate entires when going back?

Currently for my back button I'm using window.history.back() which, in itself is working properly. My issue is with an iframe that I have for refreshing sessions, example
<iframe src="https://example.com/some/path?gcp-iap-mode=SESSION_REFRESHER" style="width:0;height:0;border:0; border:none;"></iframe>
Path in my project represents current url, so it keeps changing every time I change locations. Now, with the source the iframe has, it keeps throwing 404 and getting into my history so instead of clicking back button once, I have to do it at least two times - first times it fails as it tries to reach randompath?gcp-iap-mode=SESSION_REFRESHER and then it goes to proper path.
My question is, is there a way for me to check the "back" value, to see if the "back" value has a specific query (for example gcp-iap-mode part), or check if it's the same as a current location (duplicate) so I can skip that entry (and couple of back steps) and go back to a different path in history stack?
Seems an issue like this is common in iframes. I tried looking at it from a wrong perspective, by looking for ways to change history where the only thing that needed to be changed is the iframe itself.
Basically when we only change the src attribute of the iframe it gets rendered again and again and the src gets pushed to history stack. What we needed here was the iframe to unmount and the soultion to all of that was pretty simple - add a key prop to the iframe.
This is a blog page that helped me, so if somebody wants to read more:
https://www.aleksandrhovhannisyan.com/blog/react-iframes-back-navigation-bug/

How do I automate tab selection on a website

Here is the website I am trying to access. I dont want the default tab (Day) though, I want to select the Season tab
https://www.eex.com/en/market-data/power/futures/uk-financial-futures#!/2017/05/23
The link appears to be exactly the same whichever tab is chose making differentiation impossible as far as I can tell.
Any help on this would be much appreciated, using whichever programming method and language is appropriate.
Kind Regards
Barry Walsh
The URL does not change since this is an ajax request, which you can see from MarketDataTableAController's getPageData function. You can read about them here https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started
Ive inspected your html and you seem to be using angular. On further inpection you can see that the tabs have ng-click="setActiveTab(tab)" attribute on them. So whenever user clicks, this function gets executed. It is a matter of using this function with the appropriate tab object to get the content to change. You for example could put setActiveTab(tab) into your controller init method since setActiveTab() calls the forementioned getPageData() function to update the page.
Also the tab you are looking for is page.tabs[5] ($parent.page.tabs[5] if referring from TabController) since this is the tab with the label of season. Pass that to setActiveTab() and it should show you the season instead.
However this might not be a good solution since the tab array ordering might change. Then you would need to loop over all objects in page.tabs and see if tab.label === "Season" and pass that in the function instead or better yet use the $filter service provided by angular which would look more cleaner.
Your code source also seems to be minimized and its not very easy to read.

How to use pushState,replaceState and popState correctly?

I'm trying to use the history API but I can't get it working as I'd expect. I've look at several examples but they all appear to be doing something wrong. That is, this flow doesn't work as expected:
On new page call pushState
On popState load desired page
What I'm finding happens is that for #2 the URL is not modified to the old state (testing in Firefox). Unless I call pushState, or replaceState again the URL stays where it was. I'm also finding that somehow the first page ends up duplicated in the browser's history (holding down back I can see it).
How exactly should the history be used so that Back/Forward/Reload all work like a traditional page flow?
The problems I'm having relate to the use of an iframe in the page. Each change of the location of the frame creates a new history entry which messages with the desired history. To prevent this one must replace the iframe location instead of assigning a new one.
document.getElementById('content_frame').contentWindow.location.replace( new_path )
Location.replace doesn't create a history entry.

How to set focus on tab in tabcontrol in URL

I am trying to call a page in my customers webapplication (Exact Synergy Enterprise)
This is the link: http://someserveridontdisclose/Synergy/docs/CSCANEduCourseCard.aspx?ProjectNr=ACPGINTV
Within this page is an Ajax TabContainer with several TabPanels. One of them is called 'Doelgroepen'
I dont have the source for this application, as i am not the developer of it. We only develop custom extentions to it.
Here's the question: Is it possible to focus on one of the tabs USING ONLY AN URL? If so How?
Thank you very much for your thoughts about this.
try to set with javascript. you'll have to write your own js to get index number you want from url, then set like this
$find('<%=TabContainer1.ClientID%>').set_activeTabIndex(2);
http://forums.asp.net/t/1127834.aspx
http://www.aspforums.net/Threads/420684/ASPNet-AJAX-TabContainer-Set-Active-Tab-Client-side-using-JavaScript/
If you do not have access to the code and if this is not part of the requirement / design specification for the application you are using (ie: what you asked the developer to do), then the answer is No.
The control does not have "native" support for URL tab selection. There needs to be specific code in the application in order to handle this.
It is however very easy to implement, if you absolutely need it, it shouldn't take much time (about 15-30 lines of code, depending on how many tabs/urls combination you need).
You can find a running sample of the AjaxControlToolkit Tabs control at the following link (the available functionnalities are described in there):
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/Tabs/Tabs.aspx
If the TAB has an ID you could make it visible by adding '#tabid' to the URL.

Download certain part of HTML code

I am trying to make a push notification about Internet page update, but downloading the full page (700k) again every minute is quite troublesome for users. Is there is a way to download only a specific part of page?
As far as I have read there isn't any way to get delta information about the page. Is there a method which allows that? I haven't found one for a day (if there is, how can I locate the certain byte where my information is placed in the first place?
What you want to do is learn some basics in AJAX calls.
You set an auto timer to reload div contents on whatever needs to be updated.
You could use a Range header like this:
Range: bytes=0-1000
This will obviously get you the first 1000 bytes.