首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >反应本机如何使用样式道具

反应本机如何使用样式道具
EN

Stack Overflow用户
提问于 2019-10-20 20:06:22
回答 2查看 1.3K关注 0票数 1

这是我的SettingsView组件:

代码语言:javascript
复制
<List style={styles(this.props).backgroundColorTheme}>
            <ListItem style={custom.settingsListItem} onPress={() => this.props.navigation.navigate('AppIntro')}>
              <MaterialIcons name="import-contacts" size={25} color={'#22364F'}/>
              <Text style={custom.settingsText}>
                  Покажете въвеждащата страница
              </Text>
              <Entypo name="chevron-right" size={25} style={custom.settingsDetailsArrow}/>
            </ListItem>
</List>

如何在样式属性中使用style={styles.backgroundColorTheme}而不是style=

有康斯特风格:

代码语言:javascript
复制
import {StyleSheet} from "react-native";

export const styles = (props) => StyleSheet.create({
    colorTheme: {
        color: props.theme.backgroundColor,
        marginTop: 60,
        marginBottom: 20,
        marginLeft: 20,
        fontWeight: '200',
        fontSize: 24,
    },
    backgroundColorTheme: {
        backgroundColor: props.theme.backgroundColor,
    }
});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-20 20:43:25

你有很多可能性:

  1. 使用Stylesheet.flatten(Array of styles)

代码语言:javascript
复制
<List 
    style={StyleSheet.flatten([
          styles.backgroundColorTheme, 
          {backgroundColor: 'yourcolor'}
    ]}
>
   ...
</List>

  1. --返回样式

的函数

代码语言:javascript
复制
const getListStyle = color => ({
   backgroundColor: color,
});
...
<List 
    style={getListStyle(color)}
>
   ...
</List>

对我来说最好的方法是使用styled-components

代码语言:javascript
复制
import styled from 'styled-components';

const ThemeColoredList = styled(List)`
   background-color: ${props => props.backgroundColor || 'yourdefaultcolorhere'}
`;

const Page = () => (
   <ThemeColoredList backgroundColor='yourcolorhere'>
      ...
   </ThemeColoredList>
);
票数 1
EN

Stack Overflow用户

发布于 2019-10-20 20:47:15

在您从render返回之前的SettingsView组件中,编写const themedStyles = styles(this.props);。然后您可以使用style={themedStyles.backgroundColorTheme}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58476851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档