Как переопределить CSS компонента React, используя эмоцию CSS? - PullRequest
1 голос
/ 27 апреля 2020

В следующем примере, как применить 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;
            `}
        />
    );
}

1 Ответ

1 голос
/ 27 апреля 2020

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" />
...