13

Is there a new way to use indexLink in React Router v4? I am updating some version 3 code.

I'm bringing it in with some destructuring:

var {NavLink, IndexLink} = require('react-router-dom');

and using IndexLink to keep it from being bold all the time:

<h2>Nav</h2>
<IndexLink to="/" activeClassName="active" activeStyle={{fontWeight: 'bold'}}>blah blah blah</IndexLink>
//Other navlinks working fine

IndexLink is the only thing breaking my code, here is an error from when I change it to that.

"Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in. Check the render method of Nav."

Everything is exported, and the simple app works perfectly until I add IndexLink. Now to pass out.

3
  • I'd really recommend having a proper read of the RRv4 docs - the API in the new version is fundamentally different. Commented Mar 22, 2017 at 13:52
  • 1
    burned me a time or two already Commented Mar 22, 2017 at 13:58
  • I definitely had a read before coming here. A 2 hour search through the docs and google on no sleep might not qualify as 'proper' though! :D Commented Mar 22, 2017 at 18:26

2 Answers 2

18

<Indexlink> doesn't exist in RRv4 because <IndexRoute> and the whole concept of indexes doesn't really exist in RRv4. Remember that Links and Routes are directly related.

So instead, just use <NavLink>.

Have a read about NavLink on the official docs:

NavLink - A special version of the that will add styling attributes to the rendered element when it matches the current URL.

If you need more control over the URL matching, you can use the strict or exact props.

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

1 Comment

Thanks! I still have to fix it, but that's the gist of what I needed to know. I actually had it as Navlink before trying IndexLink. Could have been sleep deprivation, but I could't find that index was gone in the documentation. Appreciate it.
12

Per Chris, the correct answer was to continue to use NavLink and use exact. Strict did not work in this case.

<NavLink exact to="/" activeClassName="active" activeStyle={{fontWeight: 'bold'}}>chris is awesome</NavLink>

Just in case another comes across this!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.