1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| 在 React Native 中,组件可以是 内置组件(如 View、Text、Button)或 自定义组件, 需要通过 import 语句导入。
1.导入 React Native 内置组件 React Native 提供了许多内置组件, 例如 View、Text、Image 等,可以直接从 react-native 导入: import { View, Text, Button } from 'react-native'; 示例:
import React from 'react'; import { View, Text, Button } from 'react-native';
const App = () => { return ( <View> <Text>Hello React Native!</Text> <Button title="Click Me" onPress={() => alert('Button Clicked!')} /> </View> ); };
export default App;
2.导入自定义组件 如果有一个 MyComponent.js 文件: import React from 'react'; import { Text } from 'react-native';
const MyComponent = () => { return <Text>This is MyComponent</Text>; };
export default MyComponent; 可以在其他文件中导入并使用: import MyComponent from './MyComponent';
const App = () => { return <MyComponent />; };
3.以 default 或 named 方式导入
3.1 默认导出(default export) import MyComponent from './MyComponent'; 3.2 命名导出(named export) import { MyComponent } from './MyComponent';
如果 MyComponent.js 这样导出 export const MyComponent = () => <Text>Named Export Component</Text>; 则需要使用 {} 进行导入 import { MyComponent } from './MyComponent';
|