A
A
aniki012018-08-19 00:47:12
React
aniki01, 2018-08-19 00:47:12

Why is react router rerendering the whole app?

There is an app file

import React from 'react'
import Header from './Header'
import Main from './Main'

const App = () => (
  <div>
    <Header />
    <Main />
  </div>
)

export default App;

header file
import React from 'react'
import { Link } from 'react-router-dom'

const Header = () => (
  <header>
    <nav>
      <ul>
        <li><Link to='/'>Home</Link></li>
        <li><Link to='/Profile'>Profile</Link></li>
      </ul>
    </nav>
  </header>
)

export default Header

main file
import React from 'react'
import { Switch, Route } from 'react-router-dom'
import Home from './Home'
import Profilefrom './Profile'

const Main = () => (
  <main>
    <Switch>
      <Route exact path='/' component={Home}/>
      <Route path='/profile' component={Profile}/>
    </Switch>
  </main>
)

export default Main

It turns out that when I click on any link, the App component is rendered along with all its children. How to avoid it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question