-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Created new React Native Components entry #1269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ace6d21
Created new React Native Components entry
ericsonrd d7c1b5e
Merge branch 'main' into rn_components
ericsonrd 0b18ab3
Update Code Block to language specific syntax
ericsonrd 423864d
Merge branch 'main' into rn_components
SSwiniarski 7698965
Merge branch 'main' into rn_components
ericsonrd 7701566
Update react-native-components.md
ericsonrd 5325386
Update react-native-components.md
ericsonrd 79a1e8d
Merge branch 'Codecademy:main' into rn_components
ericsonrd a029174
Merge branch 'Codecademy:main' into rn_components
ericsonrd db11768
Merge remote-tracking branch 'origin/main' into rn_components
ericsonrd 810e9ad
Updated React Native Components entry with corrections and change of …
ericsonrd 4482a93
Performed latest suggestions on Components entry
ericsonrd 1c60c07
Performed new sugggestions on code snippets
ericsonrd 6836e33
Performed latest suggestions on code snippets
ericsonrd 1264bb6
Merge remote-tracking branch 'origin/main' into rn_components
ericsonrd 0f6a793
Run scripts
yangc95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
content/react/concepts/react-native/terms/components/components.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
--- | ||
Title: 'Components' | ||
Description: 'Describes the appearance and behavior of the UI of a mobile application.' | ||
Subjects: | ||
- 'Mobile Development' | ||
Tags: | ||
- 'Components' | ||
- 'React Native' | ||
- 'UI' | ||
CatalogContent: | ||
- 'learn-react-native' | ||
- 'paths/front-end-engineer-career-path' | ||
--- | ||
|
||
[**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). | ||
|
||
## UI Views | ||
|
||
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. | ||
|
||
 | ||
|
||
## Native Components | ||
|
||
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. | ||
|
||
## Core Components | ||
|
||
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: | ||
|
||
| Core Component | Description | | ||
| --- | --- | | ||
| `<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. | | ||
| `<Text>` | Displays text and supports styles and touch events. It is analogous to a [paragraph element](https://www.codecademy.com/resources/docs/html/paragraphs). | | ||
| `<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). | | ||
| `<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>`] element with its `type` attribute set to `"text"`. | | ||
|`<ScrollView>` | A container that can nest multiple components and views that can scroll vertically or horizontally. It is analogous to a scrolling `div` element. | | ||
|
||
## Community Components | ||
|
||
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/). | ||
|
||
 | ||
|
||
### Examples | ||
|
||
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: | ||
|
||
```jsx | ||
import React, { Component } from 'react'; | ||
import { Text } from 'react-native'; | ||
|
||
// Functional Component | ||
const Box = () => { | ||
return ( | ||
<Text>I have a small box</Text> | ||
); | ||
} | ||
|
||
// Class Component | ||
class Box extends Component { | ||
render() { | ||
return ( | ||
<Text>I have a small box</Text> | ||
); | ||
} | ||
} | ||
ericsonrd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export default Box; | ||
``` | ||
|
||
> **Note:** To test this example, either the class or function definition of the `Box` component must be commented out before doing so. | ||
|
||
## JSX, Props and State. | ||
ericsonrd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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). | ||
|
||
### JSX | ||
|
||
As in React, the JSX syntax in React Native allows elements and variables to be written inside the JavaScript: | ||
|
||
```jsx | ||
import React from 'react'; | ||
import { Text } from 'react-native'; | ||
|
||
const Box = () => { | ||
const size = “small”; | ||
return ( | ||
<Text>I have a {size} box</Text> | ||
); | ||
} | ||
ericsonrd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export default Box; | ||
``` | ||
|
||
### Props | ||
|
||
Most core components in React Native accept props. For example, different sizes for the `Box` component can be passed via props: | ||
|
||
```jsx | ||
import React from 'react'; | ||
import { View, Text } from 'react-native'; | ||
|
||
const Box = (props) => { | ||
return ( | ||
<Text>I have a {props.size} box</Text> | ||
); | ||
} | ||
|
||
const BoxCollection = () => { | ||
return ( | ||
<View> | ||
<Box size=“small” /> | ||
<Box size=“medium” /> | ||
<Box size=“large” /> | ||
</View> | ||
); | ||
} | ||
ericsonrd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export default BoxCollection; | ||
``` | ||
|
||
### State | ||
|
||
Like in React, components in React Native also use state to handle data that changes over time, such as with user interaction: | ||
|
||
```jsx | ||
import React, { useState } from "react"; | ||
import { View, Text, Button } from "react-native"; | ||
|
||
const Box = () => { | ||
const [size, setSize] = useState("small"); | ||
return ( | ||
<View> | ||
<Text>I have a {size} box </Text> | ||
ericsonrd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<Button | ||
color="red" | ||
onPress={() => setSize("small")} | ||
title="Small" /> | ||
<Button | ||
color="blue" | ||
onPress={() => setSize("medium")} | ||
title="Medium" /> | ||
<Button | ||
color="orange" | ||
onPress={() => setSize("large")} | ||
title="Large" /> | ||
</View> | ||
); | ||
} | ||
|
||
export default Box; | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.