简体   繁体   English

React Router v4中的indexLink

[英]indexLink in React Router v4

Is there a new way to use indexLink in React Router v4? 有没有在React Router v4中使用indexLink的新方法? I am updating some version 3 code. 我正在更新一些版本3代码。

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: 并使用IndexLink始终保持粗体:

<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. IndexLink是破坏我的代码的唯一因素,这是从我将其更改为时的错误。

"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 ." “警告:React.createElement:类型无效 - 期望一个字符串(对于内置组件)或类/函数(对于复合组件)但得到:undefined。您可能忘记从其定义的文件中导出组件检查Nav的渲染方法。“

Everything is exported, and the simple app works perfectly until I add IndexLink. 一切都被导出,简单的应用程序完美运行,直到我添加IndexLink。 Now to pass out. 现在传递出来。

<Indexlink> doesn't exist in RRv4 because <IndexRoute> and the whole concept of indexes doesn't really exist in RRv4. <Indexlink>中不存在<IndexRoute> ,因为<IndexRoute>并且<Indexlink>中并不存在索引的整个概念。 Remember that Links and Routes are directly related. 请记住,链接和路由是直接相关的。

So instead, just use <NavLink> . 因此,只需使用<NavLink>

Have a read about NavLink on the official docs: 在官方文档上阅读有关NavLink的内容:

NavLink - A special version of the that will add styling attributes to the rendered element when it matches the current URL. NavLink - 一种特殊版本,它将在与当前URL匹配时为渲染元素添加样式属性。

If you need more control over the URL matching, you can use the strict or exact props. 如果您需要更多地控制URL匹配,可以使用strictexact道具。

Per Chris, the correct answer was to continue to use NavLink and use exact . Per Chris,正确答案是继续使用NavLink并使用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! 以防其他人遇到这个!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM