Attempted import error: ‘Switch‘ is not exported from ‘react-router-dom‘

created at 12-05-2021 views: 3

code

          <Router initialEntries={['/home']}>
              <Switch>
                   <Route exact path='/home' component={Home} />
                   <Route exact path='/todo' component={Todo} />
                   <Route exact path='/message' component={Message} />
                   <Route exact path='/me' component={PersonalCenter} />
              </Switch>
           </Router>

This error occurred:

Attempted import error:'Switch' is not exported from'react-router-dom'.

The reason is that after react-router-dom 6.0, Switch is no longer needed

Look at the version in package.json:

error code

solution

change to

           <Router initialEntries={['/home']}>
              <Routes>
                   <Route exact path='/home' component={Home} />
                   <Route exact path='/todo' component={Todo} />
                   <Route exact path='/message' component={Message} />
                   <Route exact path='/me' component={PersonalCenter} />
              </Routes>
           </Router>

Switch is changed to Routes

No error

Hope it helps you

Please log in to leave a comment.