# React-router-dom in Jira Cloud plugin

**URL:** <https://community.developer.atlassian.com/t/react-router-dom-in-jira-cloud-plugin/51237>\
**Category:** Jira Cloud\
**Tags:** react, atlassian-connect\
**Created:** [August 23, 2021, 3:45pm UTC](https://community.developer.atlassian.com/t/react-router-dom-in-jira-cloud-plugin/51237 "2021-08-23T15:45:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![belokurbohdan](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/belokurbohdan/32/43683_2.png) [@belokurbohdan](https://community.developer.atlassian.com/u/belokurbohdan)\
**Post date:** [August 23, 2021, 3:45pm UTC](https://community.developer.atlassian.com/t/react-router-dom-in-jira-cloud-plugin/51237/1 "2021-08-23T15:45:24Z")

</div>

Hi!  
I stuck with routing in my atlassian connect app.  
I have SPA with pages:  
**\<plugin.balse.url\>/items** - all items are displayed in table with links to every single item  
**\<plugin.balse.url\>/items/(id-of-item)** - page with single item

when I run this app locally everything is working, but when I deploy my app to my cloud instance, my base url path is changed(because my app is displayed in iframe) to  
https://(jira-site).atlassian.net/plugins/servlet/ac/my-plugin/items

All items in the table are shown correct, but when I clicked on the separate item, I recieve 404 error

I think the problem with displaying my app in iframe and (in this case) react-router wouldn’t work here.  
I’m not sure about that, so If anyone uses routing in cloud app, please help me

---

<div class="post-metadata">

**Author:** ![umang.savaliya](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/umang.savaliya/32/37943_2.png) [@umang.savaliya](https://community.developer.atlassian.com/u/umang.savaliya)\
**Post date:** [August 25, 2021, 5:44am UTC](https://community.developer.atlassian.com/t/react-router-dom-in-jira-cloud-plugin/51237/2 "2021-08-25T05:44:22Z")

</div>

We have tried with Hash tag approach in our app and it worked for us.

https://{domain}.atlassian.net/plugins/servlet/ac/{key}/app-entry?project.key=DEMO&project.id=10000#!/Manage/{newpage}

```auto
constructor(props) {
    super(props);
    if (window.AP) {
      window.AP.getLocation(location => {
        
        // Below condition is mandatory for reload application on same page
        if (location) {
          const locParts = location.split("#!");
          if (locParts.length > 1) {
            this.props.history.push(locParts[1]);
          }
        }
      });
      this.props.history.listen(location => {
        let loc = location.pathname;
        if (window.AP) {
          if (location.search) {
            loc += location.search;
          }
          const jiraState = window.AP.history.getState();
          if (loc !== jiraState) {
            window.AP.history.pushState(loc);
          }
        }
      });
      // This will listen instance hash change event and open the page accordingly
      window.AP.history.popState(e => {
        if (e && e.newURL) {
          this.props.history.push(e.newURL);
        }
      });
    }
  }

```

---

<div class="post-metadata">

**Author:** ![belokurbohdan](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/belokurbohdan/32/43683_2.png) [@belokurbohdan](https://community.developer.atlassian.com/u/belokurbohdan)\
**Post date:** [August 25, 2021, 6:10am UTC](https://community.developer.atlassian.com/t/react-router-dom-in-jira-cloud-plugin/51237/3 "2021-08-25T06:10:24Z")

</div>

Thank you! You helped me a lot!

---

<div class="post-metadata">

**Author:** ![MichaelIlewicz](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@MichaelIlewicz](https://community.developer.atlassian.com/u/MichaelIlewicz)\
**Post date:** [December 9, 2021, 11:40am UTC](https://community.developer.atlassian.com/t/react-router-dom-in-jira-cloud-plugin/51237/4 "2021-12-09T11:40:48Z")

</div>

I appreciate the response but could you be more elaborate here? I assume this is a class constructor that extends some component but which component did you extend?

---

<div class="post-metadata">

**Author:** ![ernst.stefan](https://avatars.discourse-cdn.com/v4/letter/e/7cd45c/32.png) [@ernst.stefan](https://community.developer.atlassian.com/u/ernst.stefan)\
**Post date:** [December 9, 2021, 4:01pm UTC](https://community.developer.atlassian.com/t/react-router-dom-in-jira-cloud-plugin/51237/5 "2021-12-09T16:01:41Z")

</div>

It’s not so complicated actually. Simply use HashRouter as default router from the router-dom package:

import {  
HashRouter as Router,  
Route,  
Switch,  
} from ‘react-router-dom’;

You can use react-router normally then

---

<div class="post-metadata">

**Author:** ![divineengineering](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/divineengineering/32/47537_2.png) [@divineengineering](https://community.developer.atlassian.com/u/divineengineering)\
**Post date:** [July 17, 2023, 12:32pm UTC](https://community.developer.atlassian.com/t/react-router-dom-in-jira-cloud-plugin/51237/6 "2023-07-17T12:32:31Z")

</div>

Does that preserve your location after a page reload? For instance, if I navigate to /some/path in my iframe and then reload the site, will I end up on the same page in my iframe?

---

<div class="post-metadata">

**Author:** ![ernst.stefan](https://avatars.discourse-cdn.com/v4/letter/e/7cd45c/32.png) [@ernst.stefan](https://community.developer.atlassian.com/u/ernst.stefan)\
**Post date:** [July 17, 2023, 1:43pm UTC](https://community.developer.atlassian.com/t/react-router-dom-in-jira-cloud-plugin/51237/7 "2023-07-17T13:43:07Z")

</div>

for that to work, you need to sync the hash changes with AP.history as outlined in the solution by @umang.savaliya

---

<div class="post-metadata">

**Author:** ![divineengineering](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/divineengineering/32/47537_2.png) [@divineengineering](https://community.developer.atlassian.com/u/divineengineering)\
**Post date:** [July 20, 2023, 1:47pm UTC](https://community.developer.atlassian.com/t/react-router-dom-in-jira-cloud-plugin/51237/8 "2023-07-20T13:47:02Z")

</div>

Nice, will have a look at that. Thanks.

---

<div class="post-metadata">

**Author:** ![divineengineering](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/divineengineering/32/47537_2.png) [@divineengineering](https://community.developer.atlassian.com/u/divineengineering)\
**Post date:** [July 29, 2023, 1:44pm UTC](https://community.developer.atlassian.com/t/react-router-dom-in-jira-cloud-plugin/51237/9 "2023-07-29T13:44:23Z")

</div>

I have tweaked @umang.savaliya’s answer to work with React Router v6 and Typescript. The following is all I needed to persist the location across page reloads.

```auto
import React, {PropsWithChildren, useEffect} from 'react';
import {useNavigate, useLocation} from 'react-router-dom';

declare global {
    interface Window {
        AP: any;
    }
}

interface RouterSync {
}

function RouterSync({children}: PropsWithChildren<RouterSync>) {
    const navigate = useNavigate();
    const location = useLocation();

    useEffect(() => {
        if (window.AP) {
            window.AP.getLocation((location : any) => {
                if (location) {
                    const locParts = location.split("#!");
                    if (locParts.length > 1) {
                        navigate(locParts[1]);
                    }
                }
            });
        }
    }, []);

    useEffect(() => {
        let loc = location.pathname;
        if (window.AP) {
            if (location.search) {
                loc += location.search;
            }
            const jiraState = window.AP.history.getState();
            if (loc !== jiraState) {
                window.AP.history.pushState(loc);
            }
        }
    }, [location])

    return <>{children}</>;
}

export default RouterSync;

```

You would then use it as some descendant of `HashRouter` like so:

```auto
root.render(
    <AppContainer>
            <HashRouter>
                <RouterSync>
                    <PageLayout>
                      ...

```
