官方链接

Basic Components

TextInput

  1. import React from "react";
  2. import { SafeAreaView, StyleSheet, TextInput } from "react-native";
  3. const App = () => {
  4. const [text, onChangeText] = React.useState("Useless Text");
  5. const [number, onChangeNumber] = React.useState("");
  6. return (
  7. <SafeAreaView>
  8. <TextInput
  9. style={styles.input}
  10. onChangeText={onChangeText}
  11. value={text}
  12. />
  13. <TextInput
  14. style={styles.input}
  15. onChangeText={onChangeNumber}
  16. value={number}
  17. placeholder="useless placeholder"
  18. keyboardType="numeric"
  19. />
  20. </SafeAreaView>
  21. );
  22. };
  23. const styles = StyleSheet.create({
  24. input: {
  25. height: 40,
  26. margin: 12,
  27. borderWidth: 1,
  28. padding: 10,
  29. },
  30. });
  31. export default App;

ScrollView

  1. import React from 'react';
  2. import { StyleSheet, Text, SafeAreaView, ScrollView, StatusBar } from 'react-native';
  3. const App = () => {
  4. return (
  5. <SafeAreaView style={styles.container}>
  6. <ScrollView style={styles.scrollView}>
  7. <Text style={styles.text}>
  8. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
  9. eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
  10. minim veniam, quis nostrud exercitation ullamco laboris nisi ut
  11. aliquip ex ea commodo consequat. Duis aute irure dolor in
  12. reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
  13. pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
  14. culpa qui officia deserunt mollit anim id est laborum.
  15. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
  16. eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
  17. minim veniam, quis nostrud exercitation ullamco laboris nisi ut
  18. aliquip ex ea commodo consequat. Duis aute irure dolor in
  19. reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
  20. pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
  21. culpa qui officia deserunt mollit anim id est laborum.
  22. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
  23. eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
  24. minim veniam, quis nostrud exercitation ullamco laboris nisi ut
  25. aliquip ex ea commodo consequat. Duis aute irure dolor in
  26. reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
  27. pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
  28. culpa qui officia deserunt mollit anim id est laborum.
  29. </Text>
  30. </ScrollView>
  31. </SafeAreaView>
  32. );
  33. }
  34. const styles = StyleSheet.create({
  35. container: {
  36. flex: 1,
  37. paddingTop: StatusBar.currentHeight,
  38. },
  39. scrollView: {
  40. backgroundColor: 'pink',
  41. marginHorizontal: 20,
  42. },
  43. text: {
  44. fontSize: 42,
  45. },
  46. });
  47. export default App;

StyleSheet

  1. import React from 'react';
  2. import { StyleSheet, Text, View } from 'react-native';
  3. const App = () => (
  4. <View style={container}>
  5. <Text style={text}>React Native</Text>
  6. </View>
  7. );
  8. const page = StyleSheet.create({
  9. container: {
  10. flex: 1,
  11. padding: 24,
  12. backgroundColor: '#fff',
  13. },
  14. text: {
  15. fontSize: 30,
  16. color: '#000'
  17. },
  18. });
  19. const lists = StyleSheet.create({
  20. listContainer: {
  21. flex: 1,
  22. paddingTop: 50,
  23. backgroundColor: '#61dafb',
  24. },
  25. listItem: {
  26. fontStyle: 'italic',
  27. fontWeight: 'bold'
  28. },
  29. });
  30. const container = StyleSheet.compose(page.container, lists.listContainer);
  31. const text = StyleSheet.compose(page.text, lists.listItem);
  32. export default App;

User Interface

Button

  1. import React from 'react';
  2. import { StyleSheet, Button, View, SafeAreaView, Text, Alert } from 'react-native';
  3. const Separator = () => (
  4. <View style={styles.separator} />
  5. );
  6. const App = () => (
  7. <SafeAreaView style={styles.container}>
  8. <View>
  9. <Text style={styles.title}>
  10. The title and onPress handler are required. It is recommended to set accessibilityLabel to help make your app usable by everyone.
  11. </Text>
  12. <Button
  13. title="Press me"
  14. onPress={() => Alert.alert('Simple Button pressed')}
  15. />
  16. </View>
  17. <Separator />
  18. <View>
  19. <Text style={styles.title}>
  20. Adjust the color in a way that looks standard on each platform. On iOS, the color prop controls the color of the text. On Android, the color adjusts the background color of the button.
  21. </Text>
  22. <Button
  23. title="Press me"
  24. color="#f194ff"
  25. onPress={() => Alert.alert('Button with adjusted color pressed')}
  26. />
  27. </View>
  28. <Separator />
  29. <View>
  30. <Text style={styles.title}>
  31. All interaction for the component are disabled.
  32. </Text>
  33. <Button
  34. title="Press me"
  35. disabled
  36. onPress={() => Alert.alert('Cannot press this one')}
  37. />
  38. </View>
  39. <Separator />
  40. <View>
  41. <Text style={styles.title}>
  42. This layout strategy lets the title define the width of the button.
  43. </Text>
  44. <View style={styles.fixToText}>
  45. <Button
  46. title="Left button"
  47. onPress={() => Alert.alert('Left button pressed')}
  48. />
  49. <Button
  50. title="Right button"
  51. onPress={() => Alert.alert('Right button pressed')}
  52. />
  53. </View>
  54. </View>
  55. </SafeAreaView>
  56. );
  57. const styles = StyleSheet.create({
  58. container: {
  59. flex: 1,
  60. justifyContent: 'center',
  61. marginHorizontal: 16,
  62. },
  63. title: {
  64. textAlign: 'center',
  65. marginVertical: 8,
  66. },
  67. fixToText: {
  68. flexDirection: 'row',
  69. justifyContent: 'space-between',
  70. },
  71. separator: {
  72. marginVertical: 8,
  73. borderBottomColor: '#737373',
  74. borderBottomWidth: StyleSheet.hairlineWidth,
  75. },
  76. });
  77. export default App;

Switch

  1. import React, { useState } from "react";
  2. import { View, Switch, StyleSheet, Button } from "react-native";
  3. const App = () => {
  4. const [isEnabled, setIsEnabled] = useState(false);
  5. const toggleSwitch = () => setIsEnabled(previousState => !previousState);
  6. return (
  7. <View style={styles.container}>
  8. <Button
  9. disabled={!isEnabled}
  10. title={isEnabled? 'Enabled' : 'Disabled'} />
  11. <Switch
  12. trackColor={{ false: "#767577", true: "#81b0ff" }}
  13. thumbColor={isEnabled ? "#f5dd4b" : "#f4f3f4"}
  14. ios_backgroundColor="#3e3e3e"
  15. onValueChange={toggleSwitch}
  16. value={isEnabled}
  17. />
  18. </View>
  19. );
  20. }
  21. const styles = StyleSheet.create({
  22. container: {
  23. flex: 1,
  24. flexDirection: "row",
  25. alignItems: "center",
  26. justifyContent: "space-between",
  27. marginLeft: 10,
  28. marginRight: 10,
  29. },
  30. button: {
  31. alignItems: 'center',
  32. backgroundColor: '#DDDDDD',
  33. padding: 10,
  34. marginBottom: 10
  35. }
  36. });
  37. export default App;

List Views

FlatList

  1. import React from 'react';
  2. import { SafeAreaView, View, FlatList, StyleSheet, Text, StatusBar } from 'react-native';
  3. const DATA = [
  4. {
  5. id: 'bd7acbea-c1b1-46c2-aed5-3ad53abb28ba',
  6. title: 'First Item',
  7. },
  8. {
  9. id: '3ac68afc-c605-48d3-a4f8-fbd91aa97f63',
  10. title: 'Second Item',
  11. },
  12. {
  13. id: '58694a0f-3da1-471f-bd96-145571e29d72',
  14. title: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do \
  15. eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad \
  16. minim veniam, quis nostrud exercitation ullamco laboris nisi ut \
  17. aliquip ex ea commodo consequat. Duis aute irure dolor in \
  18. reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla \
  19. pariatur. Excepteur sint occaecat cupidatat non proident, sunt in \
  20. culpa qui officia deserunt mollit anim id est laborum. \
  21. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do \
  22. eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad \
  23. minim veniam, quis nostrud exercitation ullamco laboris nisi ut \
  24. aliquip ex ea commodo consequat. Duis aute irure dolor in \
  25. reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla \
  26. pariatur. Excepteur sint occaecat cupidatat non proident, sunt in \
  27. culpa qui officia deserunt mollit anim id est laborum. \
  28. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do \
  29. eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad \
  30. minim veniam, quis nostrud exercitation ullamco laboris nisi ut \
  31. aliquip ex ea commodo consequat. Duis aute irure dolor in \
  32. reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla \
  33. pariatur. Excepteur sint occaecat cupidatat non proident, sunt in \
  34. culpa qui officia deserunt mollit anim id est laborum.',
  35. },
  36. ];
  37. const Item = ({ title }) => (
  38. <View style={styles.item}>
  39. <Text style={styles.title}>{title}</Text>
  40. </View>
  41. );
  42. const App = () => {
  43. const renderItem = ({ item }) => (
  44. <Item title={item.title} />
  45. );
  46. return (
  47. <SafeAreaView style={styles.container}>
  48. <FlatList
  49. data={DATA}
  50. renderItem={renderItem}
  51. keyExtractor={item => item.id}
  52. />
  53. </SafeAreaView>
  54. );
  55. }
  56. const styles = StyleSheet.create({
  57. container: {
  58. flex: 1,
  59. marginTop: StatusBar.currentHeight || 0,
  60. },
  61. item: {
  62. backgroundColor: '#f9c2ff',
  63. padding: 20,
  64. marginVertical: 8,
  65. marginHorizontal: 16,
  66. },
  67. title: {
  68. fontSize: 32,
  69. },
  70. });
  71. export default App;

SectionList

  1. import React from "react";
  2. import { StyleSheet, Text, View, SafeAreaView, SectionList, StatusBar } from "react-native";
  3. const DATA = [
  4. {
  5. title: "Main dishes",
  6. data: ["Pizza", "Burger", "Risotto"]
  7. },
  8. {
  9. title: "Sides",
  10. data: ["French Fries", "Onion Rings", "Fried Shrimps"]
  11. },
  12. {
  13. title: "Drinks",
  14. data: ["Water", "Coke", "Beer"]
  15. },
  16. {
  17. title: "Desserts",
  18. data: ["Cheese Cake", "Ice Cream"]
  19. }
  20. ];
  21. const Item = ({ title }) => (
  22. <View style={styles.item}>
  23. <Text style={styles.title}>{title}</Text>
  24. </View>
  25. );
  26. const App = () => (
  27. <SafeAreaView style={styles.container}>
  28. <SectionList
  29. sections={DATA}
  30. keyExtractor={(item, index) => item + index}
  31. renderItem={({ item }) => <Item title={item} />}
  32. renderSectionHeader={({ section: { title } }) => (
  33. <Text style={styles.header}>{title}</Text>
  34. )}
  35. />
  36. </SafeAreaView>
  37. );
  38. const styles = StyleSheet.create({
  39. container: {
  40. flex: 1,
  41. paddingTop: StatusBar.currentHeight,
  42. marginHorizontal: 16
  43. },
  44. item: {
  45. backgroundColor: "#f9c2ff",
  46. padding: 20,
  47. marginVertical: 8
  48. },
  49. header: {
  50. fontSize: 32,
  51. backgroundColor: "#fff"
  52. },
  53. title: {
  54. fontSize: 24
  55. }
  56. });
  57. export default App;

Others

ActivityIndicator

  1. import React from "react";
  2. import { ActivityIndicator, StyleSheet, Text, View } from "react-native";
  3. const App = () => (
  4. <View style={[styles.container, styles.horizontal]}>
  5. <ActivityIndicator />
  6. <ActivityIndicator size="large" />
  7. <ActivityIndicator size="small" color="#0000ff" />
  8. <ActivityIndicator size="large" color="#00ff00" />
  9. </View>
  10. );
  11. const styles = StyleSheet.create({
  12. container: {
  13. flex: 1,
  14. justifyContent: "center"
  15. },
  16. horizontal: {
  17. flexDirection: "row",
  18. justifyContent: "space-around",
  19. padding: 10
  20. }
  21. });
  22. export default App;

Alert

  1. import React, { useState } from "react";
  2. import { View, StyleSheet, Button, Alert } from "react-native";
  3. const App = () => {
  4. const createTwoButtonAlert = () =>
  5. Alert.alert(
  6. "Alert Title",
  7. "My Alert Msg",
  8. [
  9. {
  10. text: "Cancel",
  11. onPress: () => console.log("Cancel Pressed"),
  12. style: "cancel"
  13. },
  14. { text: "OK", onPress: () => console.log("OK Pressed") }
  15. ]
  16. );
  17. const createThreeButtonAlert = () =>
  18. Alert.alert(
  19. "Alert Title",
  20. "My Alert Msg",
  21. [
  22. {
  23. text: "Ask me later",
  24. onPress: () => console.log("Ask me later pressed")
  25. },
  26. {
  27. text: "Cancel",
  28. onPress: () => console.log("Cancel Pressed"),
  29. style: "cancel"
  30. },
  31. { text: "OK", onPress: () => console.log("OK Pressed") }
  32. ]
  33. );
  34. return (
  35. <View style={styles.container}>
  36. <Button title={"2-Button Alert"} onPress={createTwoButtonAlert} />
  37. <Button title={"3-Button Alert"} onPress={createThreeButtonAlert} />
  38. </View>
  39. );
  40. }
  41. const styles = StyleSheet.create({
  42. container: {
  43. flex: 1,
  44. justifyContent: "space-around",
  45. alignItems: "center"
  46. }
  47. });
  48. export default App;

Animated

  1. import React, { useRef } from "react";
  2. import { Animated, Text, View, StyleSheet, Button, SafeAreaView } from "react-native";
  3. const App = () => {
  4. // fadeAnim will be used as the value for opacity. Initial Value: 0
  5. const fadeAnim = useRef(new Animated.Value(0)).current;
  6. const fadeIn = () => {
  7. // Will change fadeAnim value to 1 in 5 seconds
  8. Animated.timing(fadeAnim, {
  9. toValue: 1,
  10. duration: 5000,
  11. useNativeDriver: true,
  12. }).start();
  13. };
  14. const fadeOut = () => {
  15. // Will change fadeAnim value to 0 in 3 seconds
  16. Animated.timing(fadeAnim, {
  17. toValue: 0,
  18. duration: 3000,
  19. useNativeDriver: true,
  20. }).start();
  21. };
  22. return (
  23. <SafeAreaView style={styles.container}>
  24. <Animated.View
  25. style={[
  26. styles.fadingContainer,
  27. {
  28. // Bind opacity to animated value
  29. opacity: fadeAnim
  30. }
  31. ]}
  32. >
  33. <Text style={styles.fadingText}>Fading View!</Text>
  34. </Animated.View>
  35. <View style={styles.buttonRow}>
  36. <Button title="Fade In View" onPress={fadeIn} />
  37. <Button title="Fade Out View" onPress={fadeOut} />
  38. </View>
  39. </SafeAreaView>
  40. );
  41. }
  42. const styles = StyleSheet.create({
  43. container: {
  44. flex: 1,
  45. alignItems: "center",
  46. justifyContent: "center"
  47. },
  48. fadingContainer: {
  49. padding: 20,
  50. backgroundColor: "powderblue"
  51. },
  52. fadingText: {
  53. fontSize: 28
  54. },
  55. buttonRow: {
  56. flexBasis: 100,
  57. justifyContent: "space-evenly",
  58. marginVertical: 16
  59. }
  60. });
  61. export default App;

Dimensions

  1. import React, { useState, useEffect } from "react";
  2. import { View, StyleSheet, Text, Dimensions } from "react-native";
  3. const window = Dimensions.get("window");
  4. const screen = Dimensions.get("screen");
  5. const App = () => {
  6. const [dimensions, setDimensions] = useState({ window, screen });
  7. useEffect(() => {
  8. const subscription = Dimensions.addEventListener(
  9. "change",
  10. ({ window, screen }) => {
  11. setDimensions({ window, screen });
  12. }
  13. );
  14. return () => subscription?.remove();
  15. });
  16. return (
  17. <View style={styles.container}>
  18. <Text style={styles.header}>Window Dimensions</Text>
  19. {Object.entries(dimensions.window).map(([key, value]) => (
  20. <Text>{key} : {value}</Text>
  21. ))}
  22. <Text style={styles.header}>Screen Dimensions</Text>
  23. {Object.entries(dimensions.screen).map(([key, value]) => (
  24. <Text>{key} : {value}</Text>
  25. ))}
  26. </View>
  27. );
  28. }
  29. const styles = StyleSheet.create({
  30. container: {
  31. flex: 1,
  32. justifyContent: "center",
  33. alignItems: "center"
  34. },
  35. header: {
  36. fontSize: 16,
  37. marginVertical: 10
  38. }
  39. });
  40. export default App;

KeyboardAvoidingView

  1. import React from 'react';
  2. import { View, KeyboardAvoidingView, TextInput, StyleSheet, Text, Platform, TouchableWithoutFeedback, Button, Keyboard } from 'react-native';
  3. const KeyboardAvoidingComponent = () => {
  4. return (
  5. <KeyboardAvoidingView
  6. behavior={Platform.OS === "ios" ? "padding" : "height"}
  7. style={styles.container}
  8. >
  9. <TouchableWithoutFeedback onPress={Keyboard.dismiss}>
  10. <View style={styles.inner}>
  11. <Text style={styles.header}>Header</Text>
  12. <TextInput placeholder="Username" style={styles.textInput} />
  13. <View style={styles.btnContainer}>
  14. <Button title="Submit" onPress={() => null} />
  15. </View>
  16. </View>
  17. </TouchableWithoutFeedback>
  18. </KeyboardAvoidingView>
  19. );
  20. };
  21. const styles = StyleSheet.create({
  22. container: {
  23. flex: 1
  24. },
  25. inner: {
  26. padding: 24,
  27. flex: 1,
  28. justifyContent: "space-around"
  29. },
  30. header: {
  31. fontSize: 36,
  32. marginBottom: 48
  33. },
  34. textInput: {
  35. height: 40,
  36. borderColor: "#000000",
  37. borderBottomWidth: 1,
  38. marginBottom: 36
  39. },
  40. btnContainer: {
  41. backgroundColor: "white",
  42. marginTop: 12
  43. }
  44. });
  45. export default KeyboardAvoidingComponent;

Linking

https://reactnative.dev/docs/linking

Modal

  1. import React, { useState } from "react";
  2. import { Alert, Modal, StyleSheet, Text, Pressable, View } from "react-native";
  3. const App = () => {
  4. const [modalVisible, setModalVisible] = useState(false);
  5. return (
  6. <View style={styles.centeredView}>
  7. <Modal
  8. animationType="slide"
  9. transparent={true}
  10. visible={modalVisible}
  11. onRequestClose={() => {
  12. Alert.alert("Modal has been closed.");
  13. setModalVisible(!modalVisible);
  14. }}
  15. >
  16. <View style={styles.centeredView}>
  17. <View style={styles.modalView}>
  18. <Text style={styles.modalText}>Hello World!</Text>
  19. <Pressable
  20. style={[styles.button, styles.buttonClose]}
  21. onPress={() => setModalVisible(!modalVisible)}
  22. >
  23. <Text style={styles.textStyle}>Hide Modal</Text>
  24. </Pressable>
  25. </View>
  26. </View>
  27. </Modal>
  28. <Pressable
  29. style={[styles.button, styles.buttonOpen]}
  30. onPress={() => setModalVisible(true)}
  31. >
  32. <Text style={styles.textStyle}>Show Modal</Text>
  33. </Pressable>
  34. </View>
  35. );
  36. };
  37. const styles = StyleSheet.create({
  38. centeredView: {
  39. flex: 1,
  40. justifyContent: "center",
  41. alignItems: "center",
  42. marginTop: 22
  43. },
  44. modalView: {
  45. margin: 20,
  46. backgroundColor: "white",
  47. borderRadius: 20,
  48. padding: 35,
  49. alignItems: "center",
  50. shadowColor: "#000",
  51. shadowOffset: {
  52. width: 0,
  53. height: 2
  54. },
  55. shadowOpacity: 0.25,
  56. shadowRadius: 4,
  57. elevation: 5
  58. },
  59. button: {
  60. borderRadius: 20,
  61. padding: 10,
  62. elevation: 2
  63. },
  64. buttonOpen: {
  65. backgroundColor: "#F194FF",
  66. },
  67. buttonClose: {
  68. backgroundColor: "#2196F3",
  69. },
  70. textStyle: {
  71. color: "white",
  72. fontWeight: "bold",
  73. textAlign: "center"
  74. },
  75. modalText: {
  76. marginBottom: 15,
  77. textAlign: "center"
  78. }
  79. });
  80. export default App;

PixelRatio

  1. var image = getImage({
  2. width: PixelRatio.getPixelSizeForLayoutSize(200),
  3. height: PixelRatio.getPixelSizeForLayoutSize(100)
  4. });
  5. <Image source={image} style={{ width: 200, height: 100 }} />;
  1. import React from "react";
  2. import { Image, PixelRatio, ScrollView, StyleSheet, Text, View, Dimensions } from "react-native";
  3. const size = 50;
  4. const cat = {
  5. uri: "https://reactnative.dev/docs/assets/p_cat1.png",
  6. width: size,
  7. height: size
  8. };
  9. const screen = Dimensions.get("screen");
  10. const App = () => (
  11. <View style={{flex:1,paddingTop:0}}>
  12. <ScrollView
  13. style={styles.scrollStyle}
  14. contentContainerStyle={styles.contentContainerStyle}
  15. >
  16. <View style={styles.container}>
  17. <Text>Current Pixel Ratio is:</Text>
  18. <Text style={styles.value}>{PixelRatio.get()}</Text>
  19. </View>
  20. <View style={styles.container}>
  21. <Text>Current Font Scale is:</Text>
  22. <Text style={styles.value}>{PixelRatio.getFontScale()}</Text>
  23. </View>
  24. <View style={styles.container}>
  25. <Text>On this device images with a layout width of</Text>
  26. <Text style={styles.value}>{size} px</Text>
  27. <Image source={cat} />
  28. </View>
  29. <View style={styles.container}>
  30. <Text>require images with a pixel width of</Text>
  31. <Text style={styles.value}>
  32. {PixelRatio.getPixelSizeForLayoutSize(size)} px
  33. </Text>
  34. <Image
  35. source={cat}
  36. style={{
  37. width: PixelRatio.getPixelSizeForLayoutSize(size),
  38. height: PixelRatio.getPixelSizeForLayoutSize(size)
  39. }}
  40. />
  41. </View>
  42. </ScrollView>
  43. </View>
  44. );
  45. const styles = StyleSheet.create({
  46. scrollStyle: {
  47. flex:1,
  48. },
  49. contentContainerStyle: {
  50. flex:1,
  51. justifyContent: "center",
  52. },
  53. container: {
  54. justifyContent: "center",
  55. alignItems: "center"
  56. },
  57. value: {
  58. fontSize: 24,
  59. marginBottom: 12,
  60. marginTop: 4
  61. }
  62. });
  63. export default App;

RefreshControl

  1. import React from 'react';
  2. import { RefreshControl, SafeAreaView, ScrollView, StyleSheet, Text } from 'react-native';
  3. const wait = (timeout: number) => {
  4. return new Promise(resolve => setTimeout(resolve, timeout));
  5. }
  6. const App = () => {
  7. const [refreshing, setRefreshing] = React.useState(false);
  8. const onRefresh = React.useCallback(() => {
  9. setRefreshing(true);
  10. wait(2000).then(() => setRefreshing(false));
  11. }, []);
  12. return (
  13. <SafeAreaView style={styles.container}>
  14. <ScrollView
  15. contentContainerStyle={styles.scrollView}
  16. refreshControl={
  17. <RefreshControl
  18. refreshing={refreshing}
  19. onRefresh={onRefresh}
  20. />
  21. }
  22. >
  23. <Text>Pull down to see RefreshControl indicator</Text>
  24. </ScrollView>
  25. </SafeAreaView>
  26. );
  27. }
  28. const styles = StyleSheet.create({
  29. container: {
  30. flex: 1,
  31. },
  32. scrollView: {
  33. flex: 1,
  34. backgroundColor: 'pink',
  35. alignItems: 'center',
  36. justifyContent: 'center',
  37. },
  38. });
  39. export default App;

StatusBar

  1. import React, { useState } from 'react';
  2. import { Button, Platform, SafeAreaView, StatusBar, StyleSheet, Text, View } from 'react-native';
  3. const STYLES = ['default', 'dark-content', 'light-content'];
  4. const TRANSITIONS = ['fade', 'slide', 'none'];
  5. const App = () => {
  6. const [hidden, setHidden] = useState(false);
  7. const [statusBarStyle, setStatusBarStyle] = useState(STYLES[0]);
  8. const [statusBarTransition, setStatusBarTransition] = useState(TRANSITIONS[0]);
  9. const changeStatusBarVisibility = () => setHidden(!hidden);
  10. const changeStatusBarStyle = () => {
  11. const styleId = STYLES.indexOf(statusBarStyle) + 1;
  12. if (styleId === STYLES.length) {
  13. setStatusBarStyle(STYLES[0]);
  14. } else {
  15. setStatusBarStyle(STYLES[styleId]);
  16. }
  17. };
  18. const changeStatusBarTransition = () => {
  19. const transition = TRANSITIONS.indexOf(statusBarTransition) + 1;
  20. if (transition === TRANSITIONS.length) {
  21. setStatusBarTransition(TRANSITIONS[0]);
  22. } else {
  23. setStatusBarTransition(TRANSITIONS[transition]);
  24. }
  25. };
  26. return (
  27. <View style={styles.container}>
  28. <StatusBar
  29. animated={true}
  30. backgroundColor="#61dafb"
  31. barStyle={statusBarStyle}
  32. showHideTransition={statusBarTransition}
  33. hidden={hidden} />
  34. <Text style={styles.textStyle}>
  35. StatusBar Visibility:{'\n'}
  36. {hidden ? 'Hidden' : 'Visible'}
  37. </Text>
  38. <Text style={styles.textStyle}>
  39. StatusBar Style:{'\n'}
  40. {statusBarStyle}
  41. </Text>
  42. {Platform.OS === 'ios' ? (
  43. <Text style={styles.textStyle}>
  44. StatusBar Transition:{'\n'}
  45. {statusBarTransition}
  46. </Text>
  47. ) : null}
  48. <View style={styles.buttonsContainer}>
  49. <Button
  50. title="Toggle StatusBar"
  51. onPress={changeStatusBarVisibility} />
  52. <Button
  53. title="Change StatusBar Style"
  54. onPress={changeStatusBarStyle} />
  55. {Platform.OS === 'ios' ? (
  56. <Button
  57. title="Change StatusBar Transition"
  58. onPress={changeStatusBarTransition} />
  59. ) : null}
  60. </View>
  61. </View>
  62. );
  63. };
  64. const styles = StyleSheet.create({
  65. container: {
  66. flex: 1,
  67. justifyContent: 'center',
  68. backgroundColor: '#ECF0F1'
  69. },
  70. buttonsContainer: {
  71. padding: 10
  72. },
  73. textStyle: {
  74. textAlign: 'center',
  75. marginBottom: 8
  76. }
  77. });
  78. export default App;