React Native - Votre première application
React Native - Votre première application
Section titled “React Native - Votre première application”Lorsque vous créez un nouveau projet React Native en utilisant npx react-native init YourProjectName, le fichier App.js (ou App.tsx si vous utilisez TypeScript) est le point d’entrée principal de votre application. Il ressemble généralement à ceci au départ :
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;Cette application par défaut présente plusieurs fonctionnalités de React Native, notamment la prise en charge du mode sombre, les vues de zone sécurisée (safe area views) et le contenu défilant. C’est un bon point de départ pour comprendre la structure du projet.
Création d’une application “Hello, World!”
Section titled “Création d’une application “Hello, World!””Simplifions le fichier App.js pour afficher un message “Hello, World!” basique. Remplacez l’intégralité du contenu de App.js par ce qui suit :
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;Résultat : Après avoir enregistré ces modifications, votre application (si elle est exécutée avec Fast Refresh activé) se mettra à jour pour afficher “Hello, World!” suivi de “Welcome to React Native.”, centré sur l’écran. Cet exemple simple démontre la structure de base d’un composant React Native utilisant View pour la mise en page et Text pour afficher du texte, stylisé à l’aide de StyleSheet.