React Router Without Changing URL

后端 未结 2 1038
野性不改
野性不改 2020-12-18 20:19

I need to have routing that works without changing the URL.

Before implementing this on my own, I tried to look for something by react router. I sa

相关标签:
2条回答
  • 2020-12-18 20:46

    React Router 4 has a MemoryRouter

    import { MemoryRouter } from 'react-router'
    
    <MemoryRouter>
      <App/>
    </MemoryRouter>
    

    A <Router> that keeps the history of your “URL” in memory (does not read or write to the address bar). Useful in tests and non-browser environments like React Native.

    https://reacttraining.com/react-router/web/api/MemoryRouter

    0 讨论(0)
  • 2020-12-18 21:01

    MemoryHistory is a "history provider", which you can supply to React Router like this:

    const memoryHistory = createMemoryHistory(options);
    
    // In your Router configuration
    <Router history={memoryHistory} routes={routes} />
    

    Beyond the initial configuration, everything else should work exactly the same as with regular browser history.

    This article describes how to use different providers with React Router: Histories

    0 讨论(0)
提交回复
热议问题