这是如何工作的,而不返回警告 validateDOMNesting(…)?

huangapple go评论94阅读模式
英文:

How this is working and not returning a warning validateDOMNesting(...)?

问题

我有这个问题,但我做了一个小改动,如下:

  1. children: items.map((item) => {
  2. const labelName = item.label;
  3. //DONE:将硬编码的路径更改为从后端发送的路径
  4. item.label = <NavLink to={item.key}>{labelName}</NavLink>;
  5. return {
  6. label: <div>{labelName}</div>,
  7. key: item.key,
  8. };
  9. });

没有错误,它正在工作,我的问题是为什么?

英文:

I have this problem

  1. &lt;Menu
  2. mode=&quot;horizontal&quot;
  3. items={
  4. menuState &amp;&amp;
  5. menuState?.map((valueMenus, index) =&gt; {
  6. const key = index + 1;
  7. const items = valueMenus.menusInferiores;
  8. return {
  9. key,
  10. label: (
  11. &lt;div&gt;
  12. {valueMenus.menu} &lt;DownOutlined /&gt;
  13. &lt;/div&gt;
  14. ),
  15. children: items.map((item) =&gt; {
  16. return {
  17. label: &lt;NavLink to={item.key}&gt;{labelName}&lt;/NavLink&gt;,
  18. key: item.key,
  19. };
  20. }),
  21. };
  22. })
  23. }
  24. /&gt;;

result of console:

but, I did a little change like this :

  1. children: items.map((item) =&gt; {
  2. const labelName = item.label;
  3. //DONE:Cambiar la ruta quemada por la ruta que nos envian del bk
  4. item.label = &lt;NavLink to={item.key}&gt;{labelName}&lt;/NavLink&gt;;
  5. return {
  6. label: &lt;div&gt;{labelName}&lt;/div&gt;,
  7. key: item.key,
  8. };
  9. });

no error
and it's working, my question is why ?

答案1

得分: 0

你只是将你的<NavLink/>组件用一个<div>包裹起来,这就是你所做的,而且这样做是有效的,因为根据错误提示中所述,<a>不再是<a>的子代了。要注意的是,<NavLink/>实际上是一个<a>元素,所以现在你有了:

  1. <a>
  2. <div><a></a></div>
  3. </a>
英文:

you just wrraped your &lt;NavLink/&gt; component with a &lt;div&gt; this is what you did and this is working because &lt;a&gt; is no longer a descendant of &lt;a&gt; as mentioned is the error note that &lt;NavLink/&gt; is in fact an &lt;a&gt; element so instead of :

  1. &lt;a&gt;
  2. &lt;a&gt;&lt;/a&gt;
  3. &lt;/a&gt;

now you have :

  1. &lt;a&gt;
  2. &lt;div&gt;&lt;a&gt;&lt;/a&gt;&lt;/div&gt;
  3. &lt;/a&gt;

huangapple
  • 本文由 发表于 2023年2月6日 08:41:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定