简体   繁体   English

关于使用React样式化组件对类组件进行样式化

[英]On styling of class components using React styled-components

styling components using React's styled-components. 使用React的styled-components样式化组件。 components deals with class components. 组件处理类组件。 I would like to use components (componentA) with different components (componentB) and add styles to it, but I don't know how to achieve this, so please let me know. 我想将组件(componentA)与不同的组件(componentB)一起使用,并为其添加样式,但是我不知道如何实现,所以请告诉我。

index.js index.js

import React from "react";
import ReactDOM from "react-dom";
import ComponentA from "./componentA";
import ComponentB from "./componentB";

ReactDOM.render(
    <div>
        <ComponentA/>
        <ComponentB/>
    </div>,
    document.getElementById("app")
);

componentA 组分A

import React, { Component } from "react"
import styled from "styled-components"

const Icon = styled.img`
  width: 180px;
  height: 60px;
`;

class ComponentA extends Component  {
  render() {
    return (
      <div>
        <Icon src="hoge.png" />
      </div>
    );
  }
}

export default styled(ComponentA)``;

componentB 组分B

import React from "react";
import styled from "styled-components";
import ComponentA from "./ComponentA";

const RestyleComponentA = styled(ComponentA)`
  width: 360px;
  height: 120px;
`;

export default class ComponentB extends Component {
  render() {
    <div>
      <RestyleComponentA />
    </div>
  }
}

Running the above code will look like the screenshot shown below. 运行上面的代码看起来像下面的截图。

result of execute the above code 执行以上代码的结果

But, I want this to be like this. 但是,我希望这样。

I want this to be like this 我希望这样

Why is not the style I tried to overwrite affecting either component? 为什么我尝试覆盖的样式不影响这两个组件?

That I've tried 我已经尝试过

  • Let componentA, componentB be a functional component 假设componentA,componentB为功能组件
  • Give className to "Icon element" of component A and specify style for className in styled 将className赋予组件A的“图标元素”,并为style中的className指定样式
  • Read the document of styled-components 阅读样式化组件的文档

Thank you 谢谢

You can restyle Icon, not ComponentA. 您可以重新设置Icon的样式,而不是ComponentA的样式。 Export Icon component to be reused 导出图标组件以重复使用

import React from "react";
import styled from "styled-components";
import Icon from "./Icon";

const RestyleIcon = styled(Icon)`
  width: 360px;
  height: 120px;
`;

export default class ComponentB extends Component {
  render() {
    <div>
      <RestyleIcon src="hoge.png" />
    </div>
  }
}

From Puneet Mahendra's answer, I edited each file as follows and realized style overwrite. 根据Puneet Mahendra的回答,我如下编辑了每个文件,并实现了样式覆盖。

If you find a mistake in the code I wrote, please let me know in the comments. 如果您发现我编写的代码有误,请在评论中让我知道。

index.js index.js

import React from 'react';
import ReactDOM from 'react-dom';
import ComponentA from "./componentA";
import ComponentB from "./componentB";

ReactDOM.render(
    <div>
        <ComponentA/>
        <ComponentB/>
    </div>
    , document.getElementById('root')
);

box.js box.js

import React from "react";

class Box extends React.Component {
    render() {
        return (
            <div className="box1">
                <div className="box2">
                    {this.props.children}
                </div>
            </div>
        );
    }
}

export default Box;

icon.js icon.js

import styled from "styled-components";

const Icon = styled.img`
        width: 100px;
        height: 100px;
    `;

export default Icon;

componentA 组分A

import React from "react";
import Box from "./box";
import Icon from "./icon";

class ComponentA extends React.Component {
    render() {
        return (
            <Box>
                <Icon src="hoge.png"/>
            </Box>
        );
    }
}

export default ComponentA;

componentB 组分B

import React from "react";
import styled from "styled-components";
import Icon from "./icon";
import Box from "./box";

const RestyledIcon = styled(Icon)`
width: 200px;
height: 200px;
`;

class ComponentB extends React.Component {
    render() {
        return (
            <Box>
                <RestyledIcon src="hoge.png"/>
            </Box>
        );
    }
}

export default ComponentB;

Thank you very much. 非常感谢你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM