更新反应组件上的类

发布于 2025-01-15 01:39:34 字数 473 浏览 2 评论 0原文

我有一个功能组件,并且存储中的数据发生变化,与此相关的是,组件被重绘,但类仍然是旧的,

{menu.map((option: any, index: any) => (
     <Text
         key={index}
         className={cn('menu-title', {
             'menu-active': index === 0 ?? true,
         })}
         onClick={(e) => changeActive(option, e)}
         >
         {option}
     </Text>
))}

直到“菜单活动”,我在重绘后设置了活动的第一个元素,但这只是 当 mobx-store 中的数据更改时,在初始页面加载时起作用

,菜单元素被重绘,但类仍分配给旧元素,并且菜单活动条件不起作用

I have a functional component and my data in the storage changes, in connection with this the component is redrawn, but the classes remain from the old one

{menu.map((option: any, index: any) => (
     <Text
         key={index}
         className={cn('menu-title', {
             'menu-active': index === 0 ?? true,
         })}
         onClick={(e) => changeActive(option, e)}
         >
         {option}
     </Text>
))}

through the 'menu-active', I set the active first element after redrawing, but this only works on initial page load

when the data in the mobx-store changes, the menu element is redrawn, but the class remains assigned to the old element, and the menu-active-condition does not work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的韩爷 2025-01-22 01:39:34

您的代码有点令人困惑,因为您总是通过执行 'menu-active': index === 0 将“menu-active”className 分配给第一个菜单项。另外,当 操作数的左侧要么为空,要么为未定义,因此在这种情况下,根本不需要运算符。最后一点,我假设您应该将当前菜单项索引与包含活动项索引的变量进行比较,而不是始终与零进行比较?

Your code is a bit confusing as you're always assigning the "menu-active" className to the first menu item by doing 'menu-active': index === 0. Also the ?? should be used when the left-side of the operand is either null or undefined so in this case that operator is not necessary at all. And as a final note, I'd assume you should be comparing the current menu item index to a variable that contains the active item index instead of always to zero?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文