React navigation return to any page (not integrated redux)

Source: Internet
Author: User


The


React navigation is returned by default using key as the GoBack parameter, which is generated dynamically rather than the routename we define.

There are many ways to change the source code, some say it is integrated redux. How to change the source code I tried too, but if you turn on the swipe back property, it's easy to get stuck. Integrated Redux is a good way, but for the novice redux mode is too complex, and Redux is the power of data flow control, only one because a return to learn a large framework, it is too time-consuming.

Today we do not use any third-party framework, do not modify the source code, can also be done, return to any interface.

Scenario One (NEW recommendation):

"react-navigation": "^1.5.11" The navigation in the version found in page navigationoptions is global and the navigation of the current page is printed on the page through This.props.navigation. So we can take advantage of this global navigation and then get to the global routing stack.




   Static navigation_routers;

                          Static Navigationoptions = {header: ({navigation}) =>{return



Then write a function in the base class as follows:





   React navigation returns any page

   Gobackpage (routers,pagename) {

       if (let i=0;i<routers.length;i++) {

           if ( Routers[i].routename==pagename) {

               if (i+1==routers.length) {

                   this.props.navigation.goBack (null);

                   return;

               }

               Console.log (' Executed here return ' +pagename+ ': ", Routers[i].key);

               This.props.navigation.goBack (Routers[i+1].key);

               return;}}

   ;





Use the following:





First parameter global route the second parameter to jump to the page (name is the name registered in the route)

Super.gobackpage (navigation_routers,pagename);





Support Android&&ios

Code: HTTPS://GITHUB.COM/WUYUNQIANG/RNAPP

Pro-Test effective OH.






Scenario Two:

If a->b->c->d->e sequence into the stack, we can in the navigate time the key of this page as a parameter, pass a first page, so that the next page has the key of the previous page, you can complete the return.

A->b{a.key}->c {A.key,b.key}->d{a.key,b.key,c.key}->e{a.key,b.key,c.key,d.key}

But here's the problem, this will cause key to have a dislocation, that is, if you want to return from the E-page to a.

You want to GoBack (params.keys.b);

If you are returning only one layer or using GoBack ();

Dealing with dislocation we just need to change the name of the key to the name of the previous page.

The code is as follows:





 This.props.navigation.navigate (' B ', {keys:{A_key:this.props.navigation.state.key,}});





This allows you to return to page a directly using A_key.





 This.props.navigation.navigate (' C ', {

           data:rowdata,

           keys:{...this.props.navigation.state.params.keys,b_ Key:this.props.navigation.state.key}

           })



This.props.navigation.goBack (This.props.navigation.state.params.keys. A_key y)





Reference: Https://stackoverflow.com/questions/45489343/react-navigation-back-and-goback-not-working




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.