Skip to content

React-Router only rendering the first route #6163

Closed
@jenkynolasco11

Description

@jenkynolasco11

Situation:

I've done work with React-Router before, but after updating to +16.3, all went downhill from there. Been reading in the issues here to see if I could find a similar situation, and it seems the new context api is breaking react router (as seen in this thread), but then I proceeded to do something smaller to see if there was a way to go around it, and it didn't work. Then went back into something basic, and nothing! It always renders the first Route on Switch despite the fact I'm telling Redirect to render another one.

Note: The project was created using create-react-app, so webpack configuration was not changed.

Test Case

Routes are broken. React Router always renders first route on Switch, even tho Redirect component has another route to render to render first.

Steps to reproduce

import { BrowserRouter as Router, Route, Switch, Redirect, Link } from 'react-router-dom'

class App extends Component {
    render() {
        return (
            <Router>
               <Switch>
                   <Route exact path="/ticket" component={ Comp1 } />
                   <Route exact path="/ride" component={ Comp2 } />
                   < Route exact path="/bus" component={ Comp3 } />
                   <Route exact path="/user" component={ Comp4 } />
                   <Redirect from="/*" to="/ride" />
               </Switch>
            </Router>
        )
    }
}

const Comp1 = (...props) => {
    console.log(props)
    console.log(`THIS IS COMP${1}`)
    return (
        <div>
            <Link to="/ride">TO RIDE</Link>
        </div>
    )
}
const Comp2 = (...props) => {
    console.log(props)
    console.log(`THIS IS COMP${2}`)
    return (
        <div>
            <Link to="/user">TO USER</Link>
        </div>
    )
}
const Comp3 = (...props) => {
    console.log(props)
    console.log(`THIS IS COMP${3}`)
    return (
        <div>
            <Link to="/ticket">TO TICKET</Link>
        </div>
    )
}
const Comp4 = (...props) => {
    console.log(props)
    console.log(`THIS IS COMP${4}`)
    return (
        <div>
            <Link to="/bus">TO BUS</Link>
        </div>
    )
}

Expected Behavior

First, to render the Redirect route, then switch routes when clicked on Links.

Actual Behavior

Renders first Route on Switch, even when is not the first one it should render while using Redirect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions