如何使用情感 CSS 覆盖 React 组件的 CSS?
How to override a React component's CSS using emotion CSS?
在下面的示例中,如何将 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
必须使用 className
由 css-in-js 库生成的道具(本例中的情感):
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" />
在下面的示例中,如何将 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
必须使用 className
由 css-in-js 库生成的道具(本例中的情感):
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" />