[英]Warning: validateDOMNesting(...): <a> cannot appear as a descendant of <a>
I'm trying to use react-router
with reactstrap
with create-react-app
.我正在尝试将
react-router
与reactstrap
与create-react-app
一起使用。
In the routing page, I needed to use state for the reactstrap
, so I converted the router from a variable to a class, but I get this warning:在路由页面中,我需要为
reactstrap
使用状态,因此我将路由器从变量转换为类,但我收到以下警告:
Warning: validateDOMNesting(...): <a> cannot appear as a descendant of <a>.
I don't know what to do.我不知道该怎么办。 I needed to style the router navigation using
reactstrap
, so I did this:我需要使用
reactstrap
设置路由器导航的样式,所以我这样做了:
<NavLink
componentClass={Link}
href="/contact"
to="/contact"
active={location.pathname === '/contact'}
>
anywords
</NavLink>
<Navbar dark id="RouterNavbar" expand="md">
<NavbarBrand id="NavBrand"href="#x">
<ul>
{/* a bunch of <li></li> */}
</ul>
</NavbarBrand>
<NavbarToggler id="NavBarToggler" onClick={this.toggle1.bind(this)} />
<Collapse isOpen={this.state.isOpen1} navbar>
<Nav className="ml-auto" navbar>
<NavItem>
<NavLink href="#x"><Link id="RouterNavLink" style={None} to="/contact">anywords</Link></NavLink>
</NavItem>
{/* just more of the above */}
Other than a couple of <li>
coming close to each other at random times, hot reloading not working sometimes, and the warning message I get in the console, nothing bad happens, but when I read about this issue, I found out that I shouldn't do this.除了几个
<li>
在随机时间彼此靠近,热重新加载有时无法正常工作以及我在控制台中收到警告消息之外,没有任何不好的事情发生,但是当我阅读这个问题时,我发现我不应该这样做。
This is the code which causing the error,这是导致错误的代码,
<NavLink href="#x"><Link id="RouterNavLink" style={None} to="/contact">anywords</Link></NavLink>
Which is converted to,转换为,
<a><a></a></a>
So you are getting error,所以你得到错误,
Warning: validateDOMNesting(…): <a> cannot appear as a descendant of <a>
To solve this just use one of the follow,要解决此问题,只需使用以下方法之一,
<NavLink id="RouterNavLink" style={None} to="/contact">anywords</NavLink>
OR,或者,
<Link id="RouterNavLink" style={None} to="/contact">anywords</Link>
Add the as
prop (formerly componentClass
) to your original NavLink
to keep the styling while also silencing the warning.将
as
道具(以前的componentClass
)添加到您的原始NavLink
以保持样式同时消除警告。
See react-bootstrap#nav-link-props
docs请参阅
react-bootstrap#nav-link-props
文档
Original:原来的:
<NavLink href="#x">
<Link id="RouterNavLink" style={None} to="/contact">anywords</Link>
</NavLink>
New:新的:
<Nav.Link as={Link} to="/contact">anywords</Nav.Link>
I would like to suggest an alternate solution which not only solve your problem but give you desired result.我想建议一个替代解决方案,它不仅可以解决您的问题,还可以为您提供所需的结果。 In any case someone else stumbled on this post like I did.
无论如何,其他人像我一样偶然发现了这篇文章。
Use Link jsx element offered by react router dom but add classname="nav-link"
to it.使用 react router dom 提供的 Link jsx 元素,但添加
classname="nav-link"
到它。 This will give you styling of the NavLink jsx which react strap is using.这将为您提供反应带正在使用的 NavLink jsx 的样式。
你可以试试这个以避免错误
<NavItem as="li"> <Link>.....
For the people using Gatsby.对于使用 Gatsby 的人。 If you are using a NavBar and a NavDropdown and you want to use a Link inside a NavDropdown.Item you will get an error that:
如果您正在使用 NavBar 和 NavDropdown 并且您想在 NavDropdown.Item 中使用链接,您将收到以下错误:
<a> cannot be descendant of <a>
For fixing this error try using as="li" :要修复此错误,请尝试使用 as="li" :
<NavDropdown title="Services" id="basic-nav-dropdown">
<NavDropdown.Item as="li">
<Link to="/Services" className="nav-link">
Services
</Link>
</NavDropdown.Item>
</NavDropDown>
I had same problem.我有同样的问题。 It is because we can not use link, or a tag inside another like...
这是因为我们不能使用链接,或者另一个标签里面的...
<a><a></a></a>
I wanted to apply className to my a tag in navigation bar.我想将 className 应用于导航栏中的 a 标签。 so i applied className to a tag in component and import that component in navigation bar component
所以我将className应用于组件中的标签并将该组件导入导航栏组件
<a className="nav-link" onClick={() => loginWithRedirect()}>
Login
</a>
This is how i use this link in navigation template,without any classname这就是我在导航模板中使用此链接的方式,没有任何类名
<LoginButton />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.