简体   繁体   English

如何在 React Native 中覆盖父 styles

[英]How to override parent styles in React Native

I have A container class, and i put custom made components in it with respect to container's state.我有一个容器 class,我在其中放置了与容器的 state 相关的定制组件。 In below component i defined width as width: '100%' however, in container class i have a stylesheet variable where i give paddingLeft: 20 paddingRight: 20 in order to make stateless components more shaped.在下面的组件中,我将宽度定义为width: '100%'但是,在容器 class 中,我有一个样式表变量,我在其中给出paddingLeft: 20 paddingRight: 20以使无状态组件更具形状。 this is one my stateless component:这是我的无状态组件之一:

const Seperator = props => {
    stt = {
        backgroundColor: props.backgroundColor,
    }
    textstt = {
        color: props.backgroundColor == colors.BLACK
            ? colors.WHITE
            : colors.BLACK
    }
    console.log('I am in seperator, backgroundcolor: ', props.backgroundColor, 'text', props.text, 'textcolor: ', textstt);
    return (
        <View style={[styles.container, stt]}>
            <Text style={[styles.text, textstt]}>{props.text}</Text>
        </View>
    );
};

this is how i used above component.这就是我使用上述组件的方式。

            <NewAdHoc
                contentText={'Kategori Secimi'}
                onBackPress={this.handleBackPress}
                showContentText={false}
            >
                <View style={styles.container}>
                    {currentCategory !== null
                        ? <View style={styles.flatListContainer}>
                            <ListViewItem
                                categoryName={currentCategory.categoryName}
                                iconName={currentCategory.categoryIcon}
                                showBorder={false}
                                key={currentCategory.categoryId}
                                categoryId={currentCategory.categoryId}
                                inNewAdScreen={false}
                            />
                            <Seperator
                                backgroundColor={colors.BLACK}
                                text={'Ilan Turu'}
                            />
                            {
                                currentCategory.subCategories.map((subc) => (
                                    <SubCategoryItem
                                        text={subc.subCategoryName}
                                        key={subc.subCategoryId}
                                        showBorder={true}
                                    />
                                ))

                            }
                        </View>
                        : null
                    }
                </View>
            </NewAdHoc>

but there is one thing i could not accomplish, i want my <Seperator/> not effected by styles.flatListContainer 's paddingLeft and paddingRight styles.但有一件事我无法完成,我希望我的<Seperator/>不受styles.flatListContainerpaddingLeftpaddingRight styles 的影响。

Any help will be appreciated, thanks.任何帮助将不胜感激,谢谢。

Move the padding to ListViewItem's margin.将填充移动到 ListViewItem 的边距。 assume that you can't edit style flatListContainer for some reasons and don't want to set style in ListViewItem.假设您由于某些原因无法编辑样式 flatListContainer 并且不想在 ListViewItem 中设置样式。

<NewAdHoc contentText={'Kategori Secimi'} onBackPress={this.handleBackPress} showContentText={false}>
    <View style={styles.container}>
        {currentCategory !== null ? (
            <View style={{ ...styles.flatListContainer, paddingLeft: undefined, paddingRight: undefined }}>
                <View style={{ marginHorizontal: 20 }}>
                    <ListViewItem
                        categoryName={currentCategory.categoryName}
                        iconName={currentCategory.categoryIcon}
                        showBorder={false}
                        key={currentCategory.categoryId}
                        categoryId={currentCategory.categoryId}
                        inNewAdScreen={false}
                    />
                </View>
                <Seperator backgroundColor={colors.BLACK} text={'Ilan Turu'} />
                {currentCategory.subCategories.map((subc) => (
                    <SubCategoryItem text={subc.subCategoryName} key={subc.subCategoryId} showBorder={true} />
                ))}
            </View>
        ) : null}
    </View>
</NewAdHoc>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM