【发布时间】:2018-12-02 18:50:27
【问题描述】:
theme.js - 我有一个 Styled-Components 主题,其中包含我应用的所有颜色变化。
const colors = {
purples: {
60: '#AEA',
50: '#GSA',
},
blues: {
20: '#asd',
5: '#fasd',
}
...
然后我有一个 UI 组件,我需要在其中按特定顺序定义一组颜色:
import React from 'react';
const colors = ['#GSA', '#AEA', '#asd', '#fasd', '#AEA'];
我稍后使用这个 colors 数组根据状态找到要在我的组件中使用的正确颜色:
const getBackgroundColor = ({ currentPosition }) => {
const color = colors[(currentPosition) % colors.length];
return color;
};
这个函数在我的样式组件中使用,如下所示:
const StyledCard = styled.div`
background: ${getBackgroundColor};
...
`;
问题是我的颜色数组没有设置样式组件主题。
当const colors 在样式元素之外时,如何使用我的主题定义它?
【问题讨论】:
标签: javascript css reactjs styled-components