简单组件

  1. import React from 'react'
  2. import { Text, View } from 'react-native';
  3. const App = () => {
  4. return (
  5. <View>
  6. <Text>Hello, world!</Text>
  7. </View>
  8. );
  9. }
  10. export default App;

定义Style

  1. import React from 'react'
  2. import { Text, View } from 'react-native';
  3. const App = () => {
  4. return (
  5. <View style={{
  6. flex: 1,
  7. justifyContent: "center",
  8. alignItems: "center"
  9. }}>
  10. <Text>Hello, world!</Text>
  11. </View>
  12. );
  13. }
  14. export default App;
  1. import React from 'react'
  2. import { Text, View, StyleSheet } from 'react-native';
  3. const styles = StyleSheet.create({
  4. center: {
  5. flex: 1,
  6. justifyContent: "center",
  7. alignItems: "center"
  8. }
  9. })
  10. const App = () => {
  11. return (
  12. <View style={styles.center}>
  13. <Text>Hello, world!</Text>
  14. </View>
  15. );
  16. }
  17. export default App;

自定义组件及Props

  1. import React from 'react'
  2. import { Text, View, StyleSheet } from 'react-native';
  3. const styles = StyleSheet.create({
  4. center: {
  5. flex: 1,
  6. justifyContent: "center",
  7. alignItems: "center"
  8. },
  9. Greeting: {
  10. alignItems: "center",
  11. marginTop: 50,
  12. marginBottom: 50,
  13. }
  14. })
  15. const Greeting = (props: any) => {
  16. return (
  17. <View style={styles.Greeting}>
  18. <Text>Hello {props.name}!</Text>
  19. </View>
  20. );
  21. }
  22. const App = () => {
  23. return (
  24. <View style={[styles.center, {top: 10}]}>
  25. <Greeting name='David' />
  26. <Greeting name='刘德华' />
  27. <Greeting name='盖尔·加朵' />
  28. </View>
  29. );
  30. }
  31. export default App;

State

  1. import React, { useState } from 'react'
  2. import { Text, View, StyleSheet, TouchableOpacity } from 'react-native';
  3. const styles = StyleSheet.create({
  4. container: {
  5. flex: 1,
  6. justifyContent: 'center',
  7. alignItems: 'center',
  8. },
  9. button: {
  10. alignItems: 'center',
  11. backgroundColor: '#DDDDDD',
  12. padding: 10,
  13. marginBottom: 10
  14. },
  15. redFont: {
  16. color: '#ff0000',
  17. fontSize: 20,
  18. }
  19. })
  20. const App = () => {
  21. const [count, setCount] = useState(0)
  22. const onPress = () => {
  23. setCount(count + 1)
  24. }
  25. return (
  26. <View style={styles.container}>
  27. <TouchableOpacity
  28. style={styles.button}
  29. onPress={onPress}
  30. >
  31. <Text>Click me</Text>
  32. </TouchableOpacity>
  33. <View>
  34. <Text style={styles.redFont}>
  35. You clicked { count } times
  36. </Text>
  37. </View>
  38. </View>
  39. );
  40. }
  41. export default App;

自定义独立组件

LotsOfStyles.tsx

  1. import React from 'react';
  2. import { StyleSheet, Text, View } from 'react-native';
  3. const LotsOfStylesList = () => {
  4. return (
  5. <View style={styles.container}>
  6. <Text style={styles.red}>just red</Text>
  7. <Text style={styles.bigBlue}>just bigBlue</Text>
  8. <Text style={[styles.bigBlue, styles.red]}>bigBlue, then red</Text>
  9. <Text style={[styles.red, styles.bigBlue]}>red, then bigBlue</Text>
  10. </View>
  11. );
  12. };
  13. const styles = StyleSheet.create({
  14. container: {
  15. marginTop: 50,
  16. },
  17. bigBlue: {
  18. color: 'blue',
  19. fontWeight: 'bold',
  20. fontSize: 30,
  21. },
  22. red: {
  23. color: 'red',
  24. },
  25. });
  26. export default LotsOfStylesList;

app.tsx

  1. import React from 'react'
  2. import { View } from 'react-native';
  3. import LotsOfStylesList from './LotsOfStyles';
  4. const App = () => {
  5. return (
  6. <View>
  7. <LotsOfStylesList />
  8. </View>
  9. );
  10. }
  11. export default App;