NextJS尾风警告:Prop`className`不匹配

发布于 2025-02-08 04:37:23 字数 1263 浏览 0 评论 0原文

我已经收到以下警告,我不确定如何使其消失:

Warning: Prop `className` did not match. 

 Server: "flex items-center justify-start my-2 p-4 text-sm w-full hover:text-white rounded-r-lg \n       

 font-medium text-white bg-gray-900 rounded-r-lg group flex items-center px-2 py-2" 

 Client: "flex items-center justify-start my-2 p-4 text-sm w-full hover:text-white rounded-r-lg \n            
 text-gray-200 hover:bg-gray-700 group flex items-center px-2 py-2"
 a

_c<@webpack-internal:///./node_modules/next/dist/client/link.js:133:27

我的代码中的区域是:

const style = {
  inactive: `text-gray-200 hover:bg-gray-700`,
  active: `font-medium text-white bg-gray-900 rounded-r-lg`,
  link: `flex items-center justify-start my-2 p-4 text-sm w-full hover:text-white rounded-r-lg`,
};

 function Items() {
   const { asPath } = useRouter();
   return (
     <ul>
      <li>
         {data.map((item) => (
           <Link href={item.link} key={item.title}>
            <a
               className={classNames(
                 `${style.link} 
                  ${item.link === asPath ? style.active : style.inactive}`,
            "group flex items-center px-2 py-2"
          )}
        >

编辑:添加了更多代码

I have getting the following warning, I am unsure of how to get it to go away:

Warning: Prop `className` did not match. 

 Server: "flex items-center justify-start my-2 p-4 text-sm w-full hover:text-white rounded-r-lg \n       

 font-medium text-white bg-gray-900 rounded-r-lg group flex items-center px-2 py-2" 

 Client: "flex items-center justify-start my-2 p-4 text-sm w-full hover:text-white rounded-r-lg \n            
 text-gray-200 hover:bg-gray-700 group flex items-center px-2 py-2"
 a

_c<@webpack-internal:///./node_modules/next/dist/client/link.js:133:27

The area where it is in my code is:

const style = {
  inactive: `text-gray-200 hover:bg-gray-700`,
  active: `font-medium text-white bg-gray-900 rounded-r-lg`,
  link: `flex items-center justify-start my-2 p-4 text-sm w-full hover:text-white rounded-r-lg`,
};

 function Items() {
   const { asPath } = useRouter();
   return (
     <ul>
      <li>
         {data.map((item) => (
           <Link href={item.link} key={item.title}>
            <a
               className={classNames(
                 `${style.link} 
                  ${item.link === asPath ? style.active : style.inactive}`,
            "group flex items-center px-2 py-2"
          )}
        >

EDIT: added more code

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

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

发布评论

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

评论(1

谁的新欢旧爱 2025-02-15 04:37:23

您需要删除classNames

以下是代码,请检查

const style = {
  inactive: `text-gray-200 hover:bg-gray-700`,
  active: `font-medium text-white bg-gray-900 rounded-r-lg`,
  link: `flex items-center justify-start my-2 p-4 text-sm w-full hover:text-white rounded-r-lg`,
};

 function Items() {
   const { asPath } = useRouter();
   return (
     <ul>
      <li>
         {data.map((item) => (
           <Link href={item.link} key={item.title}>
            <a
               className={
                 `${style.link} 
                  ${item.link === asPath ? style.active : style.inactive}
            group flex items-center px-2 py-2`
          )}
        >

You need to remove the classNames here

Below is the code , please check

const style = {
  inactive: `text-gray-200 hover:bg-gray-700`,
  active: `font-medium text-white bg-gray-900 rounded-r-lg`,
  link: `flex items-center justify-start my-2 p-4 text-sm w-full hover:text-white rounded-r-lg`,
};

 function Items() {
   const { asPath } = useRouter();
   return (
     <ul>
      <li>
         {data.map((item) => (
           <Link href={item.link} key={item.title}>
            <a
               className={
                 `${style.link} 
                  ${item.link === asPath ? style.active : style.inactive}
            group flex items-center px-2 py-2`
          )}
        >
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文