K
K
Kolejium2018-12-05 16:23:53
React
Kolejium, 2018-12-05 16:23:53

React Router 4. (Switch/Route/Redirect). How to switch to Route in the same Switch when redirecting?

Hi guys!
There is a category component. This is the parent component (! not inheritance !just redirects to them with passing properties) for FormCategory&ListCategory. Both components have their own base classes - FormComponent&ListComponent - in which the common logic lies.
Category code (render method):

render() {
        return <Switch>
            <Route path='/category/list' render={() => this.getCategoryListWithBinding()} />
            <Route path='/category/form/:id' render={() => this.getCategoryFormWithBinding()} />
        </Switch>
    }

App code:
render() {
      return (
          <Layout>
              <Switch>
                  <Route exact path='/' component={Home} />
                  <Route path='/counter' component={Counter} />
                  <Route path='/fetchdata' component={FetchData} />
                  <Route path='/category' history={this.history} component={CategoryComponent} />
                  <Route path='/products' history={this.history} component={ProductComponent} />
              </Switch>
          </Layout>
    );
  }

When calling /category/list & /category/form everything works fine. But, I need that if I go to /category, I will be transferred to /category/list with the parameters / settings of the component - the access addresses to the server are transmitted.
Roughly speaking, the only thing that comes to mind is to redirect to yourself and somehow change the location handles, but it's like ... stupid in my opinion.
There was an option, 3 Route, instead of Redirect, duplicate Route to /category/list, to the address /. But the address needs to change.
Thank you for your attention!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-12-05
@kolejium

render() {
  return (
    <Switch>
      <Route path='/category/list' render={() => this.getCategoryListWithBinding()} />
      <Route path='/category/form/:id' render={() => this.getCategoryFormWithBinding()} />
      <Redirect to='/category/list' />
    </Switch>
  );
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question