我用以下方式表达 jsx
模板:
// looping here
<li key={some_key} className={props.location.pathname.includes(path) && 'active'}>
<NavLink to={path}>{path.name}</NavLink>
// end loop
它工作正常,但在开发模式下,我看到很多关于非事件 li
项如何具有空 className
属性的警告,但它没有喜欢。我尝试这样做:
<li key={some_key} {props.location.pathname.includes(path.path) ? 'className="active"' : ''}>
但是编译失败。
有没有办法做到这一点,这样如果 className
为空,则不会被写入,或者 NavLink
可能会采用父属性或它会的东西也设置为事件状态?
最佳答案
您可以使用字符串插值,如下所示:
<li key={some_key} className={`${props.location.pathname.includes(path)?'active':''}`}>
关于javascript - 带字符串插值的 React.js className 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63640504/