【发布时间】:2019-07-27 00:43:09
【问题描述】:
我想问一下这个想法,循环数组然后将它们组合成 1 个样式表以响应本机。我在 expo 开发中使用 react-native。
问题:
const Spacing = StyleSheet.create({
marginTop5: {
marginTop: 5
},
marginTop10: {
marginTop: 10
},
marginTop15: {
marginTop: 15
},
marginTop20: {
marginTop: 20
},....
})
我们可以看到重复的代码被一次又一次地编写了很多次。 所以我想我需要这样写:
const spacing = ['margin', 'padding'];
const direction = ['Top', 'Bottom', 'Left', 'Right'];
const amounts = [ -100, -75, -50, -40, -30, -25, -20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 40, 50, 75, 100 ];
const amountsPositive = [ 0, 5, 10, 15, 20, 25, 30, 40, 50, 75, 100 ];
// Spacing template
_spacingMap = (space, direct, amount) => {
return space+direct+amount+' { '+space+direct+': '+amount+' }';
}
const testThis = _spacingMap();
const spacingLoop = spacing.map((space) => {
direction.map((direct) => {
amountsPositive.map((amount) => {
_spacingMap(space, direct, amount);
});
});
});
所以它会打印出与上面的间距列表相同的内容,而无需输入太多代码。但是有一个问题,我不知道如何让它在“const Spacing = StyleSheet.create({ ... });”中生成代码(反应原生样式表)。
我可以知道如何使它在样式表中工作吗?有可能吗?
谢谢
【问题讨论】:
标签: react-native