Text、Image、View等都称为react native的组件,当然你也可以自己定义组件。
自定义组件的代码如下:
import React from 'react'; import { StyleSheet, Text, View, Image } from 'react-native'; class HelloText extends React.Component { render() { return ( <Text>Hello, {this.props.name}!</Text> ); } } export default class App extends React.Component { render() { let pic = { uri: 'https://upload.wikimedia.org/wikipedia/commons/d/de/Bananavarieties.jpg' }; return ( <View style={styles.container}> <HelloText name="fashici"/> <HelloText name="my friend"/> <Text>Shake your phone to open the developer menu.</Text> <Image source={pic} style={{width: 193, height: 110}} /> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, });