【问题标题】:loading external css file just for one react component仅为一个反应组件加载外部 css 文件
【发布时间】:2019-04-24 12:08:15
【问题描述】:

我想将外部 css 应用到带有链接标签的反应组件。 但是,当我将组件包含在另一个项目中时,css 也会应用于父项目,这是正常的。

我怎样才能避免这种情况?我希望在组件上应用“父 css”,但由于组件是延迟加载的,因此 css 不会对其进行处理。

【问题讨论】:

    标签: css reactjs


    【解决方案1】:

    我发现在 react 中使用样式来避免此类问题的最佳方法是使用 styled-components(github 上有 23k+ 星)

    styled-components 使用示例:

    const Button = styled.a`
      /* This renders the buttons above... Edit me! */
      display: inline-block;
      border-radius: 3px;
      padding: 0.5rem 0;
      margin: 0.5rem 1rem;
      width: 11rem;
      background: transparent;
      color: white;
      border: 2px solid white;
    
      /* The GitHub button is a primary button
       * edit this to target it specifically! */
      ${props => props.primary && css`
        background: white;
        color: palevioletred;
      `}
    `
    
    render(
      <div>
        <Button
          href="https://github.com/styled-components/styled-components"
          target="_blank"
          rel="noopener"
          primary
        >
          GitHub
        </Button>
    
        <Button as={Link} href="/docs" prefetch>
          Documentation
        </Button>
      </div>
    )
    

    参考:https://www.styled-components.com/

     外部 CSS

    如果您使用的是外部 css,您可以使用以下方式对其进行解析:

    import { css } from "styled-components";
    
    // Css loaded as string and passed to css method of styled-components
    export const borderBottom = css`
        &:after{
            content: "";
            display: block;
            height: 3px;
            width: 200px;
            background-color: ${props => props.color || "black"};
            /* position */
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
    `;
    

    请注意,如果您想将 css 打包为字符串,则可能需要对您的 webpack 配置进行编辑。

    在 styled-components 中导入现有样式的另一种方法

    import React, { Component } from "react";
    import ReactDOM from "react-dom";
    import Styled from "styled-components";
    
    const fetchStyles = () =>
      fetch(
        "https://gist.githubusercontent.com/bionicvapourboy/61d3d7a8546cb42e0e3194eb9505f48a/raw/5432218dd83320d53d1cbc2f230b81c765183585/style.css"
      ).then(response => response.text());
    
    class App extends Component {
      state = {
        style: ""
      };
    
      componentDidMount() {
        fetchStyles().then(data => this.setState({ style: data }));
      }
    
      Wrapper = () => Styled.div`
        ${this.state.style}
      `;
      render() {
        const Wrapper = this.Wrapper();
        return (
          <div className="App">
            <Wrapper>
              <h1>Styled</h1>
            </Wrapper>
            <h1>Not Styled</h1>
            <h2>Start editing to see some magic happen!</h2>
          </div>
        );
      }
    }
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(<App />, rootElement);
    

    https://codesandbox.io/s/yw7xmx6x3x

    【讨论】:

    • 这根本没有回答问题。
    【解决方案2】:

    你可以试试这个:

    import React from 'react'
    
    let cssLoaded = false;
    
    export default class MyComponent extends React.Component {
        render() {
            if (cssLoaded === false) {
                cssLoaded = true;
                import('./MyComponentStyle.css');
            }
    
            // other stuff
        }
    }

    在你的 css 文件中: @import url('example.com/styles.css'); 来源:React: Load component's CSS only when component is rendered

    【讨论】:

    • 代码在一个单独的存储库中,所以我不能这样做。我有一个项目 A,它有自己的 css 文件,并且在那里加​​载了一个 web 组件。我希望将项目 A 的 css 应用于 web 组件
    • @stijn.aerts 你不能为你的组件创建一个自定义的 .css 文件吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    相关资源
    最近更新 更多