简体   繁体   English

aria-expanded 不适用于 nextjs 应用程序

[英]aria-expanded not working with nextjs app

Overview: I have a button that works fine in a normal index file with tailwindcss.概述:我有一个可以在带有 tailwindcss 的普通索引文件中正常工作的按钮。

Problem: I am trying to use it in my nextjs project, with typescript files instead now.问题:我正在尝试在我的 nextjs 项目中使用它,现在使用 typescript 文件。 I have an issue with the @click and aria-expanded, as it gives an error我对 @click 和 aria-expanded 有疑问,因为它给出了错误

Button按钮

<button type="button"
className="p-1 text-gray-900 transition-all duration-200 bg-transparent rounded-md hover:bg-gray-900 hover:text-white focus:outline-none focus:ring-2 focus:ring-gray-900 focus:ring-offset-2"
@click="expanded = !expanded" :aria-expanded="expanded">
<span x-show="!expanded" aria-hidden="true">
<svg className="w-6 h-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M4 6h16M4 12h16M4 18h16" />
</svg>
</span>

<span x-show="expanded" aria-hidden="true">
<svg className="w-6 h-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
</svg>
</span>
</button>

Recap: Again, this button works fine in a normal index.html file with tailwindcss, but when trying to use in nextjs typescript (tsx extension), I get the error Identifier Expected回顾:同样,这个按钮在正常的 index.html 文件和 tailwindcss 中工作正常,但是当尝试在 nextjs typescript(tsx 扩展名)中使用时,我收到错误Identifier Expected

The code that you have seems to be for another framework considering that it uses @click .考虑到它使用@click ,您拥有的代码似乎适用于另一个框架。 Since the @ and: aren't recognized by React, that leads to the Identifier Expected error.由于 React 无法识别 @ 和:,这会导致 Identifier Expected 错误。

To fix the issue, you can change :aria-expanded to aria-expanded (removing the colon), as well as change the @click function to a React equivalent, such as onClick={() => setOpen( open)} (assuming you have a state var called open ).要解决此问题,您可以将:aria-expanded更改为aria-expanded (删除冒号),并将@click function 更改为 React 等效项,例如onClick={() => setOpen( open)} (假设您有一个名为open的 state var)。

Following this, you should also make sure to replace x-show with a React equivalent, perhaps using something like {open && <>your element</>}在此之后,您还应该确保将x-show替换为 React 等效项,可能使用类似{open && <>your element</>}

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

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