gpt4 book ai didi

css - 如何使用情感 CSS 覆盖 React 组件的 CSS?

转载 作者:行者123 更新时间:2023-12-04 10:08:26 25 4
gpt4 key购买 nike

在下面的例子中,如何申请background-color:green<Test/>无需编辑 <Test/> 的组件直接组件?

/** @jsx jsx */
import { css, jsx } from "@emotion/core";
import React from "react";


function Test() {
return (
<div
css={css`
height: 100px;
width: 100px;
background-color: aqua;
`}
>
Text
</div>
);
}

function App() {
return (
<Test
css={css`
height: 400px;
width: 400px;
background-color: green;
`}
/>
);
}

最佳答案

Test 必须使用由 css-in-js 库生成的 className prop(本例中的情感):

function Test({ className }) {
return (
<div
className={className}
css={css`
height: 100px;
width: 100px;
background-color: aqua;
`}
>
Text
</div>
);
}

因此:

// Will use the styling defined in `Test` component (default)
<Text/>

// Will use the defined styling and override the default when possible,
// or add additional styling.
// i.e using background-color will override it
// using background-border will *add* a style
<Text css={...}/>

// Same as above, usually used with 3rd party css.
<Text className="someOtherCss" />

关于css - 如何使用情感 CSS 覆盖 React 组件的 CSS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61455878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com