【发布时间】:2020-10-27 00:09:25
【问题描述】:
我有一个关于样式化组件的问题,我想知道如何定位它们父母的元素,我看到有以下选项,但我不喜欢任何选项。
- 通过 props,我不喜欢这种方法,因为我认为这种方法的可维护性很糟糕,因为在复杂的情况下我们会有很多 props。
- 通过className,通常在样式化的组件中我们没有类,因为我们创建了styled.div,例如,我喜欢在我的结构中保持一致性,我不想在一些有类名,而在另一些没有。
在这种情况下 CurrentFinderLocationButton 是一个反应组件,你会如何定位它们?有没有办法在没有 className 或 props 的情况下选择它并从 StyledHome 应用样式?
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;
【问题讨论】:
标签: javascript html reactjs styled-components