2

I'm building a single page application with React and React Router.

Whenever I'm on a page, for example "/banners" and I go to "/banners/edit/1234" everything works as expected. But when I use my browser buttons to go back to "/banners" I get redirected to "/". The same things happens when I use this.props.history.goBack();. It first redirects to the previous page and then immediately redirects to /.

How can I get the user to go to "/banner" or the previous page? Or is this not possible with React Router? Or does anyone has a suggestion where to look?

import React, { Component } from 'react';
import { Switch, Route, Redirect } from 'react-router-dom'
import { PropsRoute } from 'react-router-with-props';
import Grid from '@material-ui/core/Grid';

import Content from './content.js';
import Edit from './edit.js';
import NotFound from './notfound.js';
import New from './new.js';

class Main extends Component {
  render() {
    return(
      <Grid container justify="center">
        <main>
          <Switch>
            <Route exact path="/:type" render={(props) => <Content {...props} tabs={this.props.data} />} />
            <Route exact path="/edit/:type/:id" component={Edit} />
            <Route exact path="/:type/new" component={New} />
            <Redirect exact from="/" to={"/"+this.props.data[0].toLowerCase()} />
            <Route component={NotFound} />
          </Switch>
        </main>
      </Grid>
    )
  }
}

export default Main

1 Answer 1

1

For anyway reading this question.

Alright, so the problem did not seem to be the fault of React-Router. Somewhere in my code I checked the route with an array of allowed routes. For some reason this redirected wrongly. So that explains the redirects.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.