0

I'm using react-router v4, no redux. The code example is below:

class MainPage extends Component {
    render() {
        return (
            <div className="MainPage">
              <BrowserRouter>
                <Switch>
                  {/* <Route exact path='/' /> */}
                  <Route path='/signin' component={SignIn}/>
                  <Route path='/signup' component={SignUp} />
                  <Redirect from='*' to='/' />
                </Switch>
              </BrowserRouter>
            </div>
        );
    }
}

When I'm using Link it updates URL in browser but doesn't render anything, nothing happens. When I resfresh, everything becomes fine and component renderes;

export default class Navbar extends Component {
    render() {
        return (
            <div className="navbar">
              <BrowserRouter>
                <div>
                    <Link to='/signin'>Sign in</Link>
                    <Link to='/signup'>Sign up</Link>
                </div>
              </BrowserRouter>
            </div>
        );
    }
}

I already tried everything, even withRouter(Component), but it says that with router may only be used inside How can I deal with this?

3
  • 1
    First, you should remove BrowserRouter in the NavBar, then move this Navbar component to your MainPage, and put it inside the div with the class name "MainPage" but above the BrowserRouter. Hope it works Commented Jul 26, 2018 at 15:05
  • Where do you render your Navbar Commented Jul 26, 2018 at 15:24
  • I changed the code in MainPage component and removed BrowserRouter from Navbar, and added Navbar in mainpage div above BrowserRouter, but now react tells me - You should not use <Link> outside a <Router> Commented Jul 26, 2018 at 15:31

2 Answers 2

2

Here is the working code. As others explained you should use one BrowserRouter. If you want to render your Navbar component all the time then you should place it above Switch but under BrowserRouter hence you need Link there.

const Navbar = () => (
      <div className="navbar">
            <Link to='/signin'>Sign in</Link>
            <Link to='/signup'>Sign up</Link>
      </div>
);

class MainPage extends React.Component {
  render() {
    return (
      <div className="MainPage">
        <BrowserRouter>
            <div>
                <Navbar />
                <Switch>
                    {/* <Route exact path='/' /> */}
                    <Route path='/signin' component={SignIn} />
                    <Route path='/signup' component={SignUp} />
                    <Redirect from='*' to='/' />
               </Switch>
            </div>
        </BrowserRouter>
      </div>
    );
  }
}
Sign up to request clarification or add additional context in comments.

Comments

2

You should only have a single BrowserRouter component in your tree. The BrowserRouter component holds the shared state the router used to synchronize the URL with the rendered routes. In your situation, you are getting two different versions of router state because you rendering two BrowserRouter components so you should probably render a single BrowserRouter component somewhere higher in your component tree.

If you have an App component that renders both Navbar and MainPage then you can move the router into that component:

export default class App extends Component {
    render() {
        return (
            <BrowserRouter>
              <div className="AppContainer">
                <Navbar />
                <MainPage />
              </div>
            </BrowserRouter>
        );
    }
}

3 Comments

I changed the code in MainPage component and removed BrowserRouter from Navbar, and added Navbar in mainpage div above BrowserRouter, but now react tells me - You should not use <Link> outside a <Router>
How are Navbar and MainPage rendered? Do you have other components that are rendering Navbar and MainPage?
they rendrering in App component, <Navbar /> first, then <MainPage />

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.