react router css code example

Example 1: how to add css based on route react

// do something when clicking on route 

function handler() {
  // do something;
}

<Route path='/' component={App}>
  <Route path="foo" component={Foo} onEnter={handler}/>
  <Route path="life" component={Life} onEnter={handler}/>
</Route>

Example 2: selected css based on route react

// user NavLink for conditional route styling

const Router = () => (
  <BrowserRouter>
    <div>
      <Nav>
        <NavLink exact={true} activeClassName='is-active' to='/'>Home</NavLink>
        <NavLink activeClassName='is-active' to='/about'>About</NavLink>
      </Nav>

      <Match pattern='/' exactly component={Home} />
      <Match pattern='/about' exactly component={About} />
      <Miss component={NoMatch} />
    </div>
  </BrowserRouter>
)

Tags:

Css Example