reactjs - 输出样式组件的功能
问题描述
它是一个带有样式组件的超级简单的反应应用程序。
是否有可能有一个函数来输出样式组件,我可以传递值,比如我的尝试被注释掉。
我想起诉Block
styled-component 但每次都改变颜色
import React, { Component } from 'react';
import { render } from 'react-dom';
import './style.css';
import styled from 'styled-components'
export const Block = styled.div`
width: 100px;
height: 100px;
background: red;
`
const createBlock = (col) => {
return(
Block = styled.div`
width: 100px;
height: 100px;
background: col;
`
)
}
const App = () => {
return (
<div>
<Block/>
{createBlock(red)}
</div>
);
}
render(<App />, document.getElementById('root'));
解决方案
我想你正在寻找这样的东西:
const createBlock = (col) => {
return styled.div`
width: 100px;
height: 100px;
background: ${col};
`
}
const App = () => {
const StyledBlock = createBlock('red');
return (
<StyledBlock>
<p>Some content</p>
</StyledBlock>
);
}
推荐阅读
- asp.net - 如何将带有 scss 文件的文件夹包含到 asp.net 项目或如何使用它,这可能吗?
- printing - XPS Writer 完成后启动外部 .exe
- c# - WPF 在方法内创建并显示一个新窗口并获取其结果
- django - 'NoneType' 对象在 Django 中没有属性?
- reactjs - ReactJS Props 没有加载到 componentWillMount() 和 componentDidMount()
- laravel - 数组在修改元素时无反应,仅在添加或删除时
- python - 如何在 Django 中使用 TailwindCSS?
- gmail-api - 如何将客户端应用程序添加到另一个 GSuite 公司?
- javascript - 有什么区别,什么更好用:工厂函数与构造函数。请推理您的方法,以便我学习
- flutter - 无法从 sharedPrefrence 中获取数据列表