V
V
volucris12021-04-08 19:02:19
React
volucris1, 2021-04-08 19:02:19

Is it correct to route in React via if (switch)?

switch (document.location.pathname) {
  case '/': {
    ReactDOM.render(
        <Index />,
        document.getElementById('root'),
    );
    break;
  }
  default: {
    ReactDOM.render(
        <h1>Error</h1>,
        document.getElementById('root'),
    );
  }
}

Is it worth it to do so? Just vite refuses to work with react-route-dom

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mexvod, 2021-04-08
@volucris1

If I understand correctly what you want, that is, to make a processing page if the route is not found, then this is not done this way. Use the prebuilt reactrouter library. First, define a component and a path="*" route for the 404 page. Your solution is here https://reactrouter.com/web/example/no-match
Forgot to mention and you tried https://github.com/vitejs/ vite-plugin-react-pages ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question