React Native - 应用基础
React Native - 您的第一个应用
Section titled “React Native - 您的第一个应用”当您使用 npx react-native init YourProjectName 创建一个新的 React Native 项目时,App.js(如果使用 TypeScript 则是 App.tsx)文件是您应用的主要入口点。它最初通常看起来像这样:
<html> <body> <p>import React from 'react';import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, useColorScheme, View,} from 'react-native';
import { Colors, DebugInstructions, Header, LearnMoreLinks, ReloadInstructions,} from 'react-native/Libraries/NewAppScreen'; // These are part of the default template
const Section = ({children, title}) => { const isDarkMode = useColorScheme() === 'dark'; return ( <View style={styles.sectionContainer}> <Text style={[ styles.sectionTitle, {color: isDarkMode ? Colors.white : Colors.black,}, ]}> {title} </Text> <Text style={[ styles.sectionDescription, {color: isDarkMode ? Colors.light : Colors.dark,}, ]}> {children} </Text> </View> );};
const App = () => { const isDarkMode = useColorScheme() === 'dark';
const backgroundStyle = { backgroundColor: isDarkMode ? Colors.darker : Colors.lighter, };
return ( <SafeAreaView style={backgroundStyle}> <StatusBar barStyle={isDarkMode ? 'light-content' : 'dark-content'} /> <ScrollView contentInsetAdjustmentBehavior="automatic" style={backgroundStyle}> <Header /> <View style={{ backgroundColor: isDarkMode ? Colors.black : Colors.white, }}> <Section title="Step One"> Edit <Text style={styles.highlight}>App.js</Text> to change this screen and then come back to see your edits. </Section> <Section title="See Your Changes"> <ReloadInstructions /> </Section> <Section title="Debug"> <DebugInstructions /> </Section> <Section title="Learn More"> Read the docs to discover what to do next: </Section> <LearnMoreLinks /> </View> </ScrollView> </SafeAreaView> );};
const styles = StyleSheet.create({ sectionContainer: { marginTop: 32, paddingHorizontal: 24, }, sectionTitle: { fontSize: 24, fontWeight: '600', }, sectionDescription: { marginTop: 8, fontSize: 18, fontWeight: '400', }, highlight: { fontWeight: '700', },});
export default App; </p> </body></html>这个默认应用展示了 React Native 的一些特性,包括深色模式支持、安全区域视图和可滚动内容。它是理解项目结构的一个不错的起点。
创建一个“Hello, World!”应用
Section titled “创建一个“Hello, World!”应用”让我们简化 App.js 来显示一个基本的“Hello, World!”消息。将 App.js 的全部内容替换为以下代码:
<html> <body> <p>import React from 'react';import { StyleSheet, Text, View, SafeAreaView, StatusBar } from 'react-native';
const App = () => { return ( <SafeAreaView style={styles.safeArea}> <StatusBar barStyle="dark-content" /> <View style={styles.container}> <Text style={styles.text}>Hello, World!</Text> <Text style={styles.text}>Welcome to React Native.</Text> </View> </SafeAreaView> );};
const styles = StyleSheet.create({ safeArea: { flex: 1, backgroundColor: '#f0f0f0', // Background for the whole safe area }, container: { flex: 1, // Take up all available space within SafeAreaView backgroundColor: '#fff', alignItems: 'center', // Center children horizontally justifyContent: 'center', // Center children vertically }, text: { fontSize: 20, color: '#333', marginVertical: 10, // Add some vertical spacing between text elements },});
export default App; </p> </body></html>输出:保存这些更改后,如果应用启用了 Fast Refresh,它将立即更新,显示“Hello, World!”,紧接着是“Welcome to React Native.”,它们将居中显示在屏幕上。这个简单的例子演示了 React Native 组件的基本结构,使用 View 进行布局,Text 显示文本,并通过 StyleSheet 进行样式设置。