如何在React中的对象中使用悬停

发布于 2025-01-31 10:38:04 字数 315 浏览 2 评论 0原文

我定义了一个对象USESTYLE,并在组件secondtest定义的背景颜色中调用它,是否可以在对象USESTYLE中添加悬停


const useStyle = {
  backgroundColor: "red",
  };

function SecondTest() {
  return <div style={useStyle}>SecondTest go down</div>;
}

export default SecondTest;

I defined a object useStyle and called it in the component SecondTest defined background color, is it possible to add hover in the object useStyle


const useStyle = {
  backgroundColor: "red",
  };

function SecondTest() {
  return <div style={useStyle}>SecondTest go down</div>;
}

export default SecondTest;

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

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

发布评论

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

评论(2

星星的軌跡 2025-02-07 10:38:04

您可以使用Radium React库

import React from "react";
import Radium from "radium";

const style = {
  color: "#000000",
  ":hover": {
    color: "#ffffff"
  }
};

const MyComponent = () => {
  return <section style={style}>hello world</section>;
};

const MyStyledComponent = Radium(MyComponent);

export default function App() {
  return (
    <>
      <MyStyledComponent />
    </>
  );
}

You can use Radium React Library

import React from "react";
import Radium from "radium";

const style = {
  color: "#000000",
  ":hover": {
    color: "#ffffff"
  }
};

const MyComponent = () => {
  return <section style={style}>hello world</section>;
};

const MyStyledComponent = Radium(MyComponent);

export default function App() {
  return (
    <>
      <MyStyledComponent />
    </>
  );
}
街道布景 2025-02-07 10:38:04

您可以通过onMouseEnter&amp; OnMouseLeave如果您想使用JavaScript解决您的问题。更轻松的方法将只是给出一个元素className

&lt; section className ='myclass'&gt; hello world&lt;/section&gt;

,然后只是添加所需的所需.CSS文件中的属性,您可以在组件中或全球导入。

.myClass {
  color: #000000;
}

.myClass:hover {
  color: #ffffff;
}

不可能通过在线样式添加悬停。

You can accomplish it with events like onMouseEnter & onMouseLeave if you wish to use javascript to solve your issue. Easier way to do it would be just to give an element className like

<section className='myClass'>hello world</section>

and then just to add desired properties in your .css file which you can import in your component or globally.

.myClass {
  color: #000000;
}

.myClass:hover {
  color: #ffffff;
}

It is not possible to add hover with in-line styles.

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