Review
- 2024-08-23 08:24
[!Summary] RN 默认不支持className,即不支持 CSS/SASS,仅支持 Inline Styles 和 StyleSheet. 常用功能
- Platform: Platform.OS (ios/android)
一、Introduction #
Styling in React Native is accomplished through JavaScript and uses a subset of CSS properties. Unlike CSS in web development, React Native has its own set of components and styling rules.
- StyleSheet
- Inline styles
- 手动配置 SASS
StyleSheet is a module provided by React Native to manage and optimize styles. It is similar to a CSS stylesheet and helps in creating and working with multiple styles efficiently.
StyleSheet 优势
- 元素结构和样式分离,可维护性更好;
- 样式对象可以复用,能减少重复代码;
- 样式对象只创建一次,也减少性能的损耗。
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
const App = () => {
return (
<View style={[styles.container, styles.backgroundRed]}>
<Text style={styles.text}>Hello, React Native!</Text>
<Text style={{flex: 1}}>Inline text style</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
text: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
backgroundRed: {
backgroundColor: 'red',
},
});
export default App;支持SASS #
sass-transformer https://github.com/kristerkari/react-native-sass-transformer css module https://github.com/kristerkari/react-native-css-modules
yarn add --dev react-native-sass-transformer sassLayout #
https://reactnative.dev/docs/flexbox
In React Native, layouts are primarily managed using the Flexbox styling system. Flexbox is a powerful and flexible layout system that allows you to create responsive and complex UIs using a set of simple rules.
==Flexbox== consists of three key elements: the container, main axis, and cross axis.
- The
containeris the parent flex container that holds and distributes all the child elements. - The
main axisis the primary direction of the layout (horizontal or vertical). - The
cross axisis the perpendicular direction, opposite of the main axis. flexDirection: This style specifies the primary axis using four possible values:row,row-reverse,column, orcolumn-reverse.alignItems: This style is used to align the child items along the ==cross-axis==. It uses the valuesflex-start,flex-end,center,stretch, orbaseline.justifyContent: This style is used to align the child items along the ==main axis==. It accepts the valuesflex-start,flex-end,center,space-between, orspace-around.flexWrap: Set to eitherwrapornowrapto specify if child items should wrap around to the next line when there’s not enough space on the current line.flex: This style determines how the child items grow or shrink when there’s remaining space in the container. It’s a shorthand forflex-grow,flex-shrink, andflex-basis.