reactjs - `styled` 的包装函数
问题描述
目前我正在使用带有这样样式的组件的顺风。
const Container = styled.div.attrs({
className: 'flex flex-col h-screen justify-center items-center'
})`
background-color: white;
`;
我想创建一个“包装器”函数,我可以这样调用
const Container = myStyled('div','flex flex-col h-screen justify-center items-center')`
background-color: white;
`;
const Container2 = myStyled(Container,'text-6xl')`
background-color: red;
`;
如何创建一个可以接受参数的标记模板文字字符串,然后将其传递给styled
样式化组件?
解决方案
样式化的组件可以被其他样式化的组件包裹,styled(otherStyledElement)
如下所示:
import React from "react";
import styled from 'styled-components';
export default function App() {
return (
<Container>
<h1>Hello CodeSandbox</h1>
<Container2>
<h2>Start editing to see some magic happen!</h2>
</Container2>
</Container>
);
}
const Container = styled.div.attrs({ className: 'df'})`
background: red;
`;
const Container2 = styled(Container).attrs({ className: 'df'})`
background: blue;
`;
CodeSandbox 示例:https ://codesandbox.io/s/stack-63996579-styles-stacked-thwf3
推荐阅读
- c# - “/”应用程序中的服务器错误。(没有找到您要查的资源。 )
- css - Vue.js Scoped CSS - 如何匹配根元素?
- php - PHP 位置重定向包含文件名 index.html
- javascript - 使用 Ramda 将一组 id 映射到具有这些 id 的对象
- python - pd.read_csv:utf-8'编解码器无法解码位置 61 中的字节 0x98:无效的起始字节
- javascript - 如何在异步测试中计时 sinon 间谍
- android - 如何禁用 Android Studio 3.5 按字母顺序排序 xml 标签
- r - 循环网络爬虫,都是NA,但是一步一步正常
- javascript - 保存动态添加的包含图表的 div
- python - 如何从循环中的最后一项中删除昏迷?