-
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
Changes from 4 commits
ace6d21
d7c1b5e
0b18ab3
423864d
7698965
7701566
5325386
79a1e8d
a029174
db11768
810e9ad
4482a93
1c60c07
6836e33
1264bb6
0f6a793
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,159 @@ | ||||||||||||||||||||||
--- | ||||||||||||||||||||||
Title: 'React Native Components' | ||||||||||||||||||||||
Description: 'Bundles of reusable, nestable code used to describe the appearance and behavior of a UI.' | ||||||||||||||||||||||
ericsonrd marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
Subjects: | ||||||||||||||||||||||
- 'Mobile Development' | ||||||||||||||||||||||
Tags: | ||||||||||||||||||||||
- 'Components' | ||||||||||||||||||||||
- 'React Native' | ||||||||||||||||||||||
- 'UI' | ||||||||||||||||||||||
CatalogContent: | ||||||||||||||||||||||
- 'learn-react-native' | ||||||||||||||||||||||
- 'paths/front-end-engineer-career-path' | ||||||||||||||||||||||
--- | ||||||||||||||||||||||
|
||||||||||||||||||||||
**React Native Components** are bundles of reusable, nestable code used to describe the appearance and behavior of a User Interface (UI). | ||||||||||||||||||||||
ericsonrd marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
||||||||||||||||||||||
## UI Views | ||||||||||||||||||||||
|
||||||||||||||||||||||
The basic building block of a **UI** is the **View,** a small, rectangular and often times nestable element that can be used to display text, images and respond to user input. React Native works by invoking these Views in their native environment with JavaScript, using React Components. | ||||||||||||||||||||||
yangc95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
||||||||||||||||||||||
 | ||||||||||||||||||||||
yangc95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
||||||||||||||||||||||
## Native Components | ||||||||||||||||||||||
|
||||||||||||||||||||||
These previously mentioned platform-backed components are called **Native Components,** and React Native creates the corresponding, platform specific Views (whether iOS or Android) for these components at runtime. Because of this, React Native apps look, feel and perform like Native apps. | ||||||||||||||||||||||
yangc95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
||||||||||||||||||||||
## 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: | ||||||||||||||||||||||
yangc95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
||||||||||||||||||||||
| React Native Component | Description | | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
| --- | --- | | ||||||||||||||||||||||
| \<View> | Is a container analog to a \<div> in html. It supports layout with flexbox, styles, touch handling, accessibility controls and can contain other components inside, including other views. | | ||||||||||||||||||||||
| \<Text> | Displays text, support styles and touch events. It can also nest other Text components. | | ||||||||||||||||||||||
| \<Image> | Displays different types of images, including from network, static, local disks and from ‘data:’ uri scheme. | | ||||||||||||||||||||||
| \<TextInput> | Allows the input of text by the user and provides several configuration capabilities such as auto-correction, auto-capitalization, placeholder text, etc. | | ||||||||||||||||||||||
|\<ScrollView> | An scrolling container that can nest multiple components and views. It can scroll vertically or horizontally. | | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
## Community Components | ||||||||||||||||||||||
|
||||||||||||||||||||||
React Native Componens can also be custom-built, and there’s a big ecosystem of these Community-built Components that can be accessed on the [**Native Directory**](https://reactnative.directory/) | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
## React Native Components are based on React | ||||||||||||||||||||||
|
||||||||||||||||||||||
 | ||||||||||||||||||||||
yangc95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
||||||||||||||||||||||
React Native Components share the same API structure as **React Components.** Whatever a component returns is rendered as a React element, which allows to describe what’s seen on the screen. They can also be defined as ***Function Component*** or ***Class Components:*** | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
- #### Function Component: | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
```jsx | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
import React from 'react'; | ||||||||||||||||||||||
import { Text } from 'react-native'; | ||||||||||||||||||||||
|
||||||||||||||||||||||
const Box = () => { | ||||||||||||||||||||||
return ( | ||||||||||||||||||||||
<Text>I have a small box</Text> | ||||||||||||||||||||||
); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
export default Box; | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
Try this code example [HERE.](https://snack.expo.dev/@ericsonrd/rn-function-component) | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
- #### Class Component: | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
```jsx | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
import React, { Component } from "react"; | ||||||||||||||||||||||
import { Text } from 'react-native'; | ||||||||||||||||||||||
|
||||||||||||||||||||||
class Box extends Component { | ||||||||||||||||||||||
render() { | ||||||||||||||||||||||
return ( | ||||||||||||||||||||||
<Text>I have a small box</Text> | ||||||||||||||||||||||
); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
export default Box; | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
Try this code example [HERE.](https://snack.expo.dev/@ericsonrd/rn-class-component) | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
## JSX, Props and State. | ||||||||||||||||||||||
|
||||||||||||||||||||||
React Native Components also use **JSX,** accept **Props** and manage **State.** | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
- ### JSX | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
As in React, the JSX syntax in React Native allows writing elements inside JavaScript, and also the use of variables inside them: | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
```jsx | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
import React from 'react'; | ||||||||||||||||||||||
import { Text } from 'react-native'; | ||||||||||||||||||||||
|
||||||||||||||||||||||
const Box = () => { | ||||||||||||||||||||||
const size = “small”; | ||||||||||||||||||||||
return ( | ||||||||||||||||||||||
<Text>I have a {size} box</Text> | ||||||||||||||||||||||
); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
export default Box; | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
Try this code example [HERE.](https://snack.expo.dev/@ericsonrd/rn-component-jsx) | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
- ### Props | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
Most React Native Core Components accept Props, for example, you can pass different sizes via Props to this Box component: | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
```jsx | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
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> | ||||||||||||||||||||||
); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
export default BoxCollection; | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
Try this code example [HERE.](https://snack.expo.dev/@ericsonrd/rn-component-props) | ||||||||||||||||||||||
yangc95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
||||||||||||||||||||||
- ### State | ||||||||||||||||||||||
yangc95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
Just like React, React Native Components use State to handle data that changes over time, such as with user interaction: | ||||||||||||||||||||||
yangc95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
```js | ||||||||||||||||||||||
yangc95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
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> | ||||||||||||||||||||||
<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; | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
Try this code example [HERE.](https://snack.expo.dev/@ericsonrd/rn-component-state) | ||||||||||||||||||||||
yangc95 marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.