简体   繁体   English

样式化组件中的样式化反应组件

[英]Styling react component in styled components

I have a question with styled components, I would like to know how to position elements from their parents, I saw that there are the following options but I do not like any.我对样式组件有疑问,我想知道如何从他们的父母那里获得 position 元素,我看到有以下选项,但我不喜欢任何选项。

  • Through props, I don't like this method because I consider that the maintainability of this is horrible since in complex cases we will have many props.通过 props,我不喜欢这种方法,因为我认为这种方法的可维护性很糟糕,因为在复杂的情况下我们会有很多 props。
  • Through className, generally in styled components we don't have class since we create styled.div for example, I like to have consistency in my structure and I don't want to have class names in some and not in others.通过 className,通常在样式化的组件中我们没有 class,因为我们创建了 styled.div,例如,我喜欢在我的结构中保持一致,我不想在某些而不是在其他中有 class 名称。

In this case CurrentFinderLocationButton is a react component, how would you position them?在这种情况下 CurrentFinderLocationButton 是一个反应组件,你会如何 position 他们? Is there a way to select it and apply styles from StyledHome without className or props?有没有办法 select 它并从 StyledHome 应用 styles 没有类名或道具?

import React from "react";
import styled from "styled-components";

import CurrentLocationFinderButton from "../buttons/CurrentLocationFinderButton";
import fullLogotype from "../../assets/images/full-logotype.svg";

const Home = () => {
  return (
    <StyledHome>
      <StyledLogotype src={fullLogotype} />
      <CurrentLocationFinderButton />
    </StyledHome>
  );
};

const StyledHome = styled.div`

`;

const StyledLogotype = styled.img`
  width: 150px;
  display: block;
  margin: auto;
`;

export default Home;

you can just add some styles to wrapper你可以在包装器中添加一些 styles

const StyledCurrentLocationFinderButton = styled(CurrentLocationFinderButton)`
 {any styles}
`

Finally i solved this problem binding the component class and styled components class through the props.最后我解决了这个问题,通过道具绑定组件 class 和样式组件 class 。

import React from "react";
import styled from "styled-components";
import fullLogotype from "../../assets/images/full-logotype.svg";
import CurrentLocationFinderButton from "../buttons/CurrentLocationFinderButton";
import AddressFinder from "../finders/AddressFinder";

const Logotype = ({ className }) => {
  return <img className={className} alt="" src={fullLogotype} />;
};

const EntryText = ({ className }) => {
  return (
    <p className={className}>
      Atisbalo es una app donde podrás encontrar información sobre tus locales
      favoritos y enterarte de todas las promociones que oferta tu cuidad al
      instante
    </p>
  );
};
const Home = ({ className }) => {
  return (
    <StyledHome className={className}>
      <StyledLogotype />
      <StyleEntryText />
      <StyledCurrentLocationFinderButton />
      <StyledAddressFinder/>
    </StyledHome>
  );
};

const StyledHome = styled.div``;

const StyledLogotype = styled(Logotype)`
  width: 150px;
  display: block;
  margin: auto auto 30px auto;
`;

const StyleEntryText = styled(EntryText)`
  display: block;
  width: 90%;
  text-align: center;
  margin: auto auto 30px auto;
`;

const StyledCurrentLocationFinderButton = styled(CurrentLocationFinderButton)`
  display: block;
  margin: auto auto 30px auto;
`;

const StyledAddressFinder = styled(AddressFinder)`
  width: 80%;
  margin: auto;
`
export default Home;

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

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