T
T
TheRevan2018-06-23 09:32:02
JavaScript
TheRevan, 2018-06-23 09:32:02

How to make a redirect in a component that knows nothing about route?

There is a main component:

ReactDOM.render(
  <BrowserRouter>
    <Switch>
      <Route exact  path='/' render={() => (isAuth() ? (<Redirect to="/orders"/>) : (<Redirect to="/login"/>))}/>
      <Route exact  path='/products' render={() => (isAuth() ? (<Redirect to="/orders"/>) : (<Redirect to="/login"/>))}/>
      <Route exact  path='/orders' render={() => (isAuth() ? (<PageOrders/>) : (<Redirect to="/login"/>))}/>
      <Route exact  path='/add_order' render={() => (isAuth() ? (<PageAddOrder/>) : (<Redirect to="/login"/>))}/>
      <Route exact  path='/login' render={() => (isAuth() ? (<Redirect to="/orders"/>) : (<Autorize/>))}/>
    </Switch>
  </BrowserRouter>,
document.getElementById('root'));

At the same time, the component that is loaded in Render (Any, for example, PageOrders or PageAddOrder) contains a structure with a menu, footer and content, the content has its own component, which changes depending on the selected page. The bottom line is that I need, for example, on the authorization page, after successful authorization, go to the main page or to the order page, how can I do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Максим, 2018-06-23
@TheRevan

Здесь уже можно улучшить, сделав HOC компонент - PrivateRoute
Про редирект (в экшене вызывается callback) + запрос в компоненте.
p.s. Это все было решено на вебинаре. Пример (решение тестового задания, видео-разбор)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question