2

This is my code which I don't think is right and redundant:

<BrowserRouter>
 <Route exact={true} path="/" component={App}/>
 <Route path="/:filter" component={App}/>
</BrowserRouter>

I think exact={true} is redundant since I can simply do path="/(:filter)" in previous react router versions? I don't wanna use history.push :(

This is how I usd my NavLink in my Footer Component:

const Footer = () => (
  <p>
    Show:
    {" "}
    <FilterLink filter="all"> 
      All 
    </FilterLink>
    {", "}
    <FilterLink filter="active">
      Active
    </FilterLink>
    {", "}
    <FilterLink filter="completed">
      Completed
    </FilterLink>
  </p>
);

and my FilterLink:

const FilterLink = ({ filter, children }) => (
  <NavLink 
    to={ filter === 'all' ? '' : filter }
    activeStyle={{
      textDecoration: 'none',
      color: 'red'
    }}
  >
  {children}
  </NavLink>
);

The path is changing, example: localhost:3000/active but the style has no effect, but it affects the all? when I am on localhost:3000/?

1 Answer 1

2

In react-router v4 you can defined an optional path parameter like

<BrowserRouter>
 <Route path="/:filter?" component={App}/>
</BrowserRouter>

Path parameter must be defined in a manner which is as per the npm package path-to-regexp understands as mentioned in the react-router docs

Sign up to request clarification or add additional context in comments.

7 Comments

when I am applying activeStyle, it is not working on my localhost:3000 and not on other filters?
what do you mean by when you are applying activeStyle
If you don't add active style then
all have the same style, just adds underline below, but I want to add activeStyle which I cannot accomplish
maybe <NavLink to={ filter === 'all' ? '/' : '/' + filter } activeStyle={{ textDecoration: 'none', color: 'red' }} >
|

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.