|
| 1 | +--- |
| 2 | +Title: 'Components' |
| 3 | +Description: 'Describes the appearance and behavior of the UI of a mobile application.' |
| 4 | +Subjects: |
| 5 | + - 'Mobile Development' |
| 6 | +Tags: |
| 7 | + - 'Components' |
| 8 | + - 'React Native' |
| 9 | + - 'UI' |
| 10 | +CatalogContent: |
| 11 | + - 'learn-react-native' |
| 12 | + - 'paths/front-end-engineer-career-path' |
| 13 | +--- |
| 14 | + |
| 15 | +[**Components**](https://www.codecademy.com/resources/docs/react/components) are units of reusable code that describe the appearance and behavior of a mobile application's [user interface (UI)](https://www.codecademy.com/resources/docs/uiux/ui-design). |
| 16 | + |
| 17 | +## UI Views |
| 18 | + |
| 19 | +Represented as small, rectangular, and oftentimes nestable elements, views can display text, media, and respond to user input. React Native invokes views in their native environment with JavaScript. |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +## Native Components |
| 24 | + |
| 25 | +These previously mentioned platform-backed components are called Native Components. Whether iOS or Android, React Native creates the corresponding, platform-specific view(s) for these components at runtime. Therefore, React Native apps look, feel, and perform like Native apps. |
| 26 | + |
| 27 | +## Core Components |
| 28 | + |
| 29 | +React Native offers a set of essential, ready-to-use native components called core components. There are many components ranging from text to activity indicators. Most apps will use these core components: |
| 30 | + |
| 31 | +| Core Component | Description | |
| 32 | +| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 33 | +| `<View>` | A common container component that supports layout with flexbox, styles, touch handling, accessibility controls, and can contain other components inside such as other views. It is analogous to a non-scrolling [`<div>`](https://www.codecademy.com/resources/docs/html/elements/div) HTML element. | |
| 34 | +| `<Text>` | Displays text and supports styles and touch events. It is analogous to a [paragraph element](https://www.codecademy.com/resources/docs/html/paragraphs). | |
| 35 | +| `<Image>` | Displays different types of images, including from network, static, local disks, and from ‘data:’ [URI](https://www.codecademy.com/resources/docs/general/uri) scheme. It is analogous to an [image element](https://www.codecademy.com/resources/docs/html/images). | |
| 36 | +| `<TextInput>` | Allows the input of text by the user and provides several configuration capabilities such as auto-correction, auto-capitalization, placeholder text, etc. It is analogous to an [`<input>`](https://www.codecademy.com/resources/docs/html/elements/input) element with its `type` attribute set to `"text"`. | |
| 37 | +| `<ScrollView>` | A container that can nest multiple components and views that can scroll vertically or horizontally. It is analogous to a scrolling `div` element. | |
| 38 | + |
| 39 | +## Community Components |
| 40 | + |
| 41 | +Components can also be custom-built; there’s a big ecosystem of these community-built components that can be accessed on the [Native Directory](https://reactnative.directory/). |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +### Examples |
| 46 | + |
| 47 | +React Native uses the same component syntax structure for its views to display elements to the screen, like in [React.js](https://www.codecademy.com/resources/docs/react). The following examples are of a `Box` component defined as both a class and functional component: |
| 48 | + |
| 49 | +```jsx |
| 50 | +import React, { Component } from 'react'; |
| 51 | +import { Text } from 'react-native'; |
| 52 | + |
| 53 | +// Functional Component |
| 54 | +const Box = () => { |
| 55 | + return <Text>I have a small box</Text>; |
| 56 | +}; |
| 57 | + |
| 58 | +// Class Component |
| 59 | +class Box extends Component { |
| 60 | + render() { |
| 61 | + return <Text>I have a small box</Text>; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +export default Box; |
| 66 | +``` |
| 67 | + |
| 68 | +> **Note:** To test this example, either the class or function definition of the `Box` component must be commented out before doing so. |
| 69 | +
|
| 70 | +## JSX, Props, and State |
| 71 | + |
| 72 | +Components also use [JSX](https://www.codecademy.com/resources/docs/react/jsx), accept [props](https://www.codecademy.com/resources/docs/react/props), and manage [state](https://www.codecademy.com/resources/docs/react/state). |
| 73 | + |
| 74 | +### JSX |
| 75 | + |
| 76 | +As in React, the JSX syntax in React Native allows elements and variables to be written inside the JavaScript: |
| 77 | + |
| 78 | +```jsx |
| 79 | +import React from 'react'; |
| 80 | +import { Text } from 'react-native'; |
| 81 | + |
| 82 | +const Box = () => { |
| 83 | + const size = “small”; |
| 84 | + return ( |
| 85 | + <Text>I have a {size} box</Text> |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +export default Box; |
| 90 | +``` |
| 91 | + |
| 92 | +### Props |
| 93 | + |
| 94 | +Most core components in React Native accept props. For example, different sizes for the `Box` component can be passed via props: |
| 95 | + |
| 96 | +```jsx |
| 97 | +import React from 'react'; |
| 98 | +import { View, Text } from 'react-native'; |
| 99 | + |
| 100 | +const Box = (props) => { |
| 101 | + return ( |
| 102 | + <Text>I have a {props.size} box</Text> |
| 103 | + ); |
| 104 | +} |
| 105 | + |
| 106 | +const BoxCollection = () => { |
| 107 | + return ( |
| 108 | + <View> |
| 109 | + <Box size=“small” /> |
| 110 | + <Box size=“medium” /> |
| 111 | + <Box size=“large” /> |
| 112 | + </View> |
| 113 | + ); |
| 114 | +} |
| 115 | + |
| 116 | +export default BoxCollection; |
| 117 | +``` |
| 118 | + |
| 119 | +### State |
| 120 | + |
| 121 | +Like in React, components in React Native also use state to handle data that changes over time, such as with user interaction: |
| 122 | + |
| 123 | +```jsx |
| 124 | +import React, { useState } from 'react'; |
| 125 | +import { View, Text, Button } from 'react-native'; |
| 126 | + |
| 127 | +const Box = () => { |
| 128 | + const [size, setSize] = useState('small'); |
| 129 | + return ( |
| 130 | + <View> |
| 131 | + <Text>I have a {size} box</Text> |
| 132 | + <Button color="red" onPress={() => setSize('small')} title="Small" /> |
| 133 | + <Button color="blue" onPress={() => setSize('medium')} title="Medium" /> |
| 134 | + <Button color="orange" onPress={() => setSize('large')} title="Large" /> |
| 135 | + </View> |
| 136 | + ); |
| 137 | +}; |
| 138 | + |
| 139 | +export default Box; |
| 140 | +``` |
0 commit comments