[英]How can I change the color of a third-party React component?
I am using a React library called vertical-timeline-component-react
.我正在使用一个名为
vertical-timeline-component-react
的 React 库。 https://developer.aliyun.com/mirror/npm/package/vertical-timeline-component-react https://developer.aliyun.com/mirror/npm/package/vertical-timeline-component-react
<Fragment>
<Timeline>
<Content>
<ContentYear
startMonth="12"
monthType="text"
startDay="24"
startYear="2016"
/>
<ContentBody style={{color: 'red'}} title="Amazing Title">
</ContentBody>
</Content>
<Content>
<ContentYear
startMonth="12"
monthType="text"
startDay="24"
startYear="2016"
/>
<ContentBody title="Amazing Title">
</ContentBody>
</Content>
</Timeline>
</Fragment>
I used the sample code provided in the example, which renders two Content
components.我使用了示例中提供的示例代码,它呈现了两个
Content
组件。 What I want to do is to change the color of the text in the ContentBody
and make the color different for each component.我想要做的是更改
ContentBody
中文本的颜色,并使每个组件的颜色不同。
As you can see, I tried using inline styling <ContentBody style={{color: 'red'}} title="Amazing Title">
, but it didn't work.如您所见,我尝试使用内联样式
<ContentBody style={{color: 'red'}} title="Amazing Title">
,但没有成功。
Then I tried to inspect the element and found out that the className of the text body is .jertxS
.然后我尝试检查元素,发现文本正文的 className 是
.jertxS
。 I then created a styles.css
to change the font color of the class.然后我创建了一个
styles.css
来更改 class 的字体颜色。 It worked, but it applies to all ContentBody
s.它有效,但适用于所有
ContentBody
。 What I want is to have them different colors.我想要的是让它们不同 colors。
It seems like the library doesn't provide a template for doing it.看起来图书馆没有提供模板来做这件事。 In this case, what is the way to customize the color?
在这种情况下,自定义颜色的方法是什么?
When in doubt, check the source code.如有疑问,请检查源代码。 We live in a wonderful time where most npm packages you install have public source code.
我们生活在一个美好的时代,您安装的大多数 npm 软件包都有公共源代码。 This library's source is ongithub and you can view what the ContentBody component is doing here
该库的源代码在github上,您可以在此处查看 ContentBody 组件在做什么
const ContentBody = (props) => {
const { title, children } = props;
return (
<BodyComponent className='body-component'>
<BodyComponentTitle className='title-body-component'>
{title}
</BodyComponentTitle>
{children}
</BodyComponent>
);
};
It looks like they are using a static className of 'body-component' that you might be able to target.看起来他们正在使用您可以定位的“body-component”的 static 类名。 It does not appear that they are passing style or className thru from props so you have to work with what you got, or fork and extend the library to meet your needs and optionally push back your changes so others can benefit.
他们似乎没有通过 props 传递 style 或 className,所以你必须使用你得到的东西,或者 fork 和扩展库以满足你的需求,并且可以选择推迟你的更改,以便其他人可以受益。
Depending on how radical your changes are you can also just copy the src files into your project and start altering and consuming them directly.根据您的更改的激进程度,您还可以将 src 文件复制到您的项目中并开始直接更改和使用它们。 The project is MIT licensed so your are free to embed the code in your app.
该项目是 MIT 许可的,因此您可以自由地将代码嵌入到您的应用程序中。
Careful using that class, as it may change on your next build.小心使用 class,因为它可能会在您的下一个版本中改变。
First check if they have any colour prop in their API.首先检查他们的 API 中是否有任何颜色道具。 If not, check if passing in a “className” will stick.
如果没有,请检查是否会传递“className”。 If not, use a more complex css selector like 'div > div:nth-of-type >...'
如果没有,请使用更复杂的 css 选择器,例如 'div > div:nth-of-type >...'
Like @Alan P said, try adding a className attribute and write a class in your css file.就像@Alan P 说的那样,尝试添加一个 className 属性并在 css 文件中写入 class 。
component.js组件.js
<ContentBody className="red-color" title="Amazing Title">
</ContentBody>
index.css索引.css
.red-color{ color: red !important }
That should override the current color styling那应该覆盖当前的颜色样式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.