【问题标题】:Flexbox in React NativeReact Native 中的 Flexbox
【发布时间】:2021-05-30 19:21:00
【问题描述】:

我有一个SafeAreaView,我正在尝试对其应用一些样式。

如果我使用内联样式,我的文本会在屏幕上居中。

const thisStyle = {
        flex: 1,
        flexDirection: 'column',
        justifyContent: 'center',
        alignItems: 'center'
}

    <SafeAreaView  style={thisStyle}>
      ...
    </SafeAreaView>

但是,如果我在 scss 文件中定义样式并将其导入到我的组件中,则不会应用弹性框。

import styles from './login.scss';

...


<SafeAreaView  style={styles.container}>
  ...
</SafeAreaView>

scss:

@import './src/base.scss';

    .container {
        display: flex;
        width: 100%;
        height: 100%;
        background-color: $plump-purple;
        align-items: 'center';
        justify-content: 'center'
    }

请注意,其他属性可以正常工作(背景改变颜色),唯一看起来不工作的是 flexbox。我错过了什么吗?

【问题讨论】:

    标签: css react-native sass flexbox


    【解决方案1】:

    以这种方式使用它。

    主类

    import React, { Component } from 'react';
    import { Text, StyleSheet, View } from 'react-native';
    import styles from './styles';
    
    export default class Example extends Component {
      render() {
        return <SafeAreaView style={styles.container}>...</SafeAreaView>;
      }
    }
    

    样式类

    import { StyleSheet } from 'react-native';
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        width: '100%',
        height: '100%',
        backgroundColor: 'plump-purple',
        alignItems: 'center',
        justifyContent: 'center',
      },
    });
    

    【讨论】:

    • 我更新了答案,请检查您使用的是不推荐的 scss。更新具有实现样式的正确方法。像这样使用它。
    • 样式类是否应该与主类在同一个文件中?或者我可以为每个组件创建一个 Style.js 文件吗?通用标准是什么?
    • 两种方式都可以。如果您有大型样式文件,那么您可以将每种样式用于一个组件。
    猜你喜欢
    • 2015-06-15
    • 2018-08-15
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 2015-08-05
    • 2017-10-10
    • 2020-12-13
    • 1970-01-01
    相关资源
    最近更新 更多