我使用的是react+tailwindcss v3,但我不知道如何更改其父元素hover上的SVG笔划 colored颜色 .

这是我在其中呈现SVG component的代码

import Arrow from "../../assets/logos/Arrow_up_right";

const MyComponent= () => {
    const onhover = "blue";
    const normal = "red";

    return (
       <ul className='list-none px-2'>
          <li> className='my-2 h-12 w-12 flex items-center justify-center rounded-lg bg-gray-100 hover:cursor-pointer'>
             <Arrow stroke={normal} />
          </li>
       </ul>
};

Arrow是返回SVG的组件.我已经给它传递了一个prop,然后用它来提供colorarrow个SVG的行程.

下面是SVG组件:

const Arrow_up_right = ({ stroke }) => {
    return (
        <svg
            width='8'
            height='8'
            viewBox='0 0 8 8'
            fill='none'
            xmlns='http://www.w3.org/2000/svg'
        >
            <path
                d='M0.666626 7.33335L7.33329 0.666687M7.33329 0.666687H0.666626M7.33329 0.666687V7.33335'
                stroke={stroke}
                stroke-linecap='round'
                stroke-linejoin='round'
            />
        </svg>
    );
};

export default Arrow_up_right;

103是我想要传递的prop的值应该在lihover上改变, 如果不能做到这一点,请建议一些其他方法.

任何帮助都将不胜感激.:)

推荐答案

CSS VariablesTailwind Utilities中try 这种方法,我们将初始值从父组件传递给子组件,然后在父组件<li>中使用一个额外的类.

enter image description here

index.css

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer utilities {
  .arrow svg path {
    stroke: var(--normal);
  }
  ,
  .arrow:hover svg path {
    stroke: var(--hover);
  }
}

Arrow.jsx

export const Arrow = ({ strokeColor, strokeHover }) => {
  return (
    <svg width="8" height="8" viewBox="0 0 8 8" fill="none" xmlns="http://www.w3.org/2000/svg">
      <path
        d="M0.666626 7.33335L7.33329 0.666687M7.33329 0.666687H0.666626M7.33329 0.666687V7.33335"
        style={{ '--normal': strokeColor, '--hover': strokeHover }}
        strokeLinecap="round"
        strokeLinejoin="round"
      />
    </svg>
  );
};

App.jsx

function App() {
  const onhover = 'blue';
  const normal = 'red';

  return (
    <div className="p-6">
      <ul className="list-none px-2">
        <li className="my-2 h-12 w-12 flex items-center justify-center rounded-lg bg-gray-100 hover:cursor-pointer arrow hover:arrow">
          <Arrow strokeColor={normal} strokeHover={onhover} />
        </li>
      </ul>
    </div>
  );
}

Javascript相关问答推荐

d3可排序表标题行中有收件箱--如何使收件箱不触发排序?

带对角线分隔符的图像滑动器

访客柜台的风格React.js

JavaScript:循环访问不断变化的数组中的元素

单击按钮后未清除 Select

如何避免使用ajax在Vue 3合成API中重定向

在JavaScript中对大型级联数组进行切片的最有效方法?

将数据从strapi提取到next.js,但响应延迟API URL

传递一个大对象以在Express布局中呈现

从mat—country—select获取整个Country数组

未捕获错误:在注销后重定向到/login页面时找不到匹配的路由

在带有背景图像和圆形的div中添加长方体阴影时的重影线

使用GraphQL查询Uniswap的ETH价格

我在Django中的视图中遇到多值键错误

ChartJs未呈现

当使用';字母而不是与';var#39;一起使用时,访问窗口为什么返回未定义的?

为什么123[';toString';].long返回1?

更改预请求脚本中重用的JSON主体变量- Postman

Jest toHaveBeenNthCalledWith返回当前设置的变量值,而不是调用时的值

try 将Redux工具包与MUI ToggleButtonGroup组件一起使用时出错