React Router: не менять URL для указанных c страниц - PullRequest
2 голосов
/ 09 июля 2020

Есть ли способ не изменять URL-адрес для некоторых страниц?

Я знаю, что с помощью «MemoryRouter» я могу переключаться на другие страницы с помощью history.push без изменения URL-адреса вообще. Однако я хочу изменить URL-адрес некоторых страниц. Есть ли способ сделать это?

Текущий код:

import {Route, BrowserRouter as Router, Switch} from 'react-router-dom';
import {createMemoryHistory} from 'history';

return (
    <Router basename="/d/home_page" history={history}>
              <Switch>
                  // I want to change the URL for this page
                  <Route path="/products">
                    <Products />
                  </Route>
                  // I want to change the URL for this page
                  <Route path="/files">
                    <Files />
                  </Route>
                  // I do not want to change the URL for this page
                  <Route path="/do_not_change_url">
                    <DoNotChangeURL />
                  </Route>

1 Ответ

0 голосов
/ 09 июля 2020

Вы пробовали это сделать?

import {Route, BrowserRouter as Router, MemoryRouter} from 'react-router-dom';
import {createMemoryHistory} from 'history';
...
return (
    <Router basename="/d/home_page" history={history}>
      <div>
          // I want to change the URL for this page
          <Route path="/products">
            <Products />
          </Route>
          // I want to change the URL for this page
          <Route path="/files">
            <Files />
          </Route>
          <MemoryRouter>
             <Route path="/do_change_url">
               <DoNotChangeURL />
             </Route>
          </MemoryRouter>
      <div>
    <Router

или

import {Route, BrowserRouter as Router, Switch, MemoryRouter} from 'react-router-dom';
import {createMemoryHistory} from 'history';
...
return (
    <Router basename="/d/home_page" history={history}>
      <Switch>
          // I want to change the URL for this page
          <Route path="/products">
            <Products />
          </Route>
          // I want to change the URL for this page
          <Route path="/files">
            <Files />
          </Route>
          <MemoryRouter>
             <Route path="/do_change_url">
               <DoNotChangeURL />
             </Route>
          </MemoryRouter>
      <Switch>
    <Router
...