Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing custom browser history into IonReactRouter #20297

Open
mogusbi opened this issue Jan 25, 2020 · 6 comments
Open

Passing custom browser history into IonReactRouter #20297

mogusbi opened this issue Jan 25, 2020 · 6 comments
Assignees
Projects

Comments

@mogusbi
Copy link

@mogusbi mogusbi commented Jan 25, 2020

Ionic version:

[x] 4.x
[ ] 5.x

I'm submitting a ...

[ ] bug report
[x] feature request

Current behavior:
IonReactRouter doesn't give you the option to pass in a custom browser history. It would be great if you could do so like you can if you use the Router component from react-router-dom (https://reacttraining.com/react-router/web/api/Router)

Expected behavior:
To be able to use custom history like so

// history.ts
import { createBrowserHistory } from 'history';

const history = createBrowserHistory();

export default history;

// App.tsx
import { IonReactRouter } from '@ionic/react-router';
import history from './history';

const App: FC = () => (
  <IonReactRouter history={history}>
    {...}
  </IonReactRouter>
)
@ionitron-bot ionitron-bot bot added the triage label Jan 25, 2020
@Mageek627
Copy link

@Mageek627 Mageek627 commented Jan 26, 2020

I agree with you, and I am interested in a solution for that. FYI, I had recently put a bounty on an old question on Stackoverflow that you might wanna check https://stackoverflow.com/questions/55884643/ionic-4-routing-with-angular-6

Basically the situation, as I understand it, is the following:

  1. There was a way to set browser history (or at least, navigation history) in Ionic

  2. There is not anymore

  3. An Angular (or React) solution is not enough to solve the problem

@seanharr11
Copy link

@seanharr11 seanharr11 commented Jan 26, 2020

Another concrete use case: user needs to listen for the appUrlOpen to intercept a Universal Link, and navigate to that component, e.g. :

# history.ts
import { createBrowserHistory } from 'history';
import { History } from 'history';

export type ReadonlyBrowserHistory = Readonly<History>
export const browserHistory: ReadonlyBrowserHistory = createBrowserHistory();
# App.tsx
import { browserHistory } from './history';
...
     <IonApp>
        <IonReactRouter history={browserHistory}>
          <IonRouterOutlet id='main' key={String(user)}>
            <Route path='/' render={() => user === null ? <LoggedOutRouter /> : <LoggedInRouter />} />
          </IonRouterOutlet>
        </IonReactRouter>
        <Menu />
      </IonApp>
# File listening for Universal Link events
import {browserHistory} from './history'

IonicApp.addListener("appUrlOpen",` (data) => {
  browserHistory.push(data.url)
}

To my knowledge there's not a clean way to handle this.

@elylucas
Copy link
Member

@elylucas elylucas commented Jan 27, 2020

IonReactRouter currently wraps BrowserRouter, which doesn't accept pre-built history.

I'll look into changing it to wrap Router instead and provide the browser stuff ourselfs.

@ali-master
Copy link

@ali-master ali-master commented Mar 7, 2020

When we can apply a custom browser history to the IonReactRouter component?

@rvanmil
Copy link

@rvanmil rvanmil commented May 7, 2020

In case anyone finds this issue and is disappointed this hasn't been implemented yet, here's a quick workaround which seems to work for us.

import React from 'react'
import { History } from 'history'
import { useHistory } from 'react-router'

// Add custom property 'appHistory' to the global window object
declare global {
  interface Window { appHistory: History }
}

const MyApp: React.FC = () => {
  // Store the history object globally so we can access it outside of React components
  window.appHistory = useHistory()

  ...
}

You are now able to access and mutate the history from anywhere in your app using the window.appHistory property.

@mogusbi
Copy link
Author

@mogusbi mogusbi commented Jun 14, 2020

Hi @elylucas, are there any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Ionic React
  
Awaiting triage
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
7 participants
You can’t perform that action at this time.