Kirill
Kirill

Reputation: 217

React router issue: typing any url in address bar redirects to /

I'm having an issue with react-router. If I type random route in address bar (mysite.com/users (valid url to users page) or mysite.com/adf (definitely not valid url), which should show 404page), router redirects me to home page ('/'). If I click on Link which leads on same page, everything is fine, the necessary page is shown.

  {
    path: '/',
    exact: true,
    cache: false,
    component: HomePage,
  },
  {
    path: '/users',
    exact: true,
    cache: false,
    component: UsersPage,
  },
  {
    path: '*',
    exact: true, // I tried both true and false
    cache: false,
    component: NotFoundPage,
    // also I tried to leave only component here
  },

then I render routes with renderRoutes func from react-router-config

  <Switch location={location}>
    {renderRoutes(routes, { ...someParams })}
  </Switch>

I want router not to redirect me and show needed page if I type in address bar valid url, like mysite.com/users and to show not found page, if I type in address bar invalid url, like mysite.com/asd

UPD: The problem is only with ConnectedRouter, if I use react-router-dom Router everything is fine

Upvotes: 2

Views: 1993

Answers (1)

Kirill
Kirill

Reputation: 217

So the issue was in Switch. You don't have to use it, because renderRoutes has it's own Switch and you can pass switch params as third argument, like {renderRoutes(routes, { ...someParams }, { ...switchParams })}

Upvotes: 1

Related Questions