简体   繁体   English

如何解析'<navlink classname="{({" isactive })> 活跃? “红色”:“蓝色”} /&gt;'?</navlink>

[英]How to parse '<NavLink className={({ isActive }) => isActive ? "red" : "blue"} />'?

I think I understand why the first instance of isActive that's passed as an argument to the arrow function is encapsulated in curly brackets, ie { isActive } , while the second isActive is not encapsulated in curly brackets, but would like to be sure I understand correctly.我想我理解为什么作为参数传递给箭头 function 的第一个isActive实例被封装在大括号中,即{ isActive } ,而第二个isActive没有被封装在大括号中,但想确保我理解正确. Is the first "isActive" that's encapsulated in curly brackets ie, ({ isActive }) an example of Object Destructuring?封装在大括号中的第一个“isActive”,即({ isActive })是 Object 解构的示例吗? (If not, doesn't an arrow function in JS expect to receive the name of a variable, but doesn't { isActive } evaluate to true or false ?) (如果不是,JS 中的箭头 function 是否期望接收变量的名称,但{ isActive }不会评估为truefalse ?)

Many thanks in advance if anyone can please confirm whether this is an example of Object Destructuring.非常感谢,如果有人可以确认这是否是 Object 解构的示例。 Or if I've got this wrong, then many thanks if you can please explain!或者如果我有这个错误,那么如果你能解释一下,非常感谢!

Your understanding is right.你的理解是对的。 react-router v6 has changed the NavLink API a bit. react-router v6稍微更改了NavLink API。 You need to provide the isActive wrapped in an object as opposed to in NavLink v5 .您需要提供包装在 object 中的isActive ,而不是在NavLink v5中。

export interface NavLinkProps
  extends Omit<LinkProps, "className" | "style" | "children"> {
  children:
    | React.ReactNode
    | ((props: { isActive: boolean }) => React.ReactNode);
  caseSensitive?: boolean;
  className?: string | ((props: { isActive: boolean }) => string | undefined);
  end?: boolean;
  style?:
    | React.CSSProperties
    | ((props: { isActive: boolean }) => React.CSSProperties);
}

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

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