Skip to content
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

Created new React Native Components entry #1269

Merged
merged 16 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
---
Title: 'React Native Components'
ericsonrd marked this conversation as resolved.
Show resolved Hide resolved
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.
ericsonrd marked this conversation as resolved.
Show resolved Hide resolved

![React Native to Native UI Representation](https://github.com/Codecademy/docs/tree/main/media/react_native_UI_Views.png)

## 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.
ericsonrd 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:

| React Native Component | Description |
Copy link
Contributor

@Dusch4593 Dusch4593 Dec 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| React Native Component | Description |
| Native Component | Description |

| --- | --- |
| \<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. |
Copy link
Contributor

Choose a reason for hiding this comment

The 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. |
| `<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

React Native Components 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/)
Copy link
Contributor

@Dusch4593 Dusch4593 Dec 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
React Native Components 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/)
Components 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/).


## React Native Components are based on React

![React and React Native Components](https://github.com/Codecademy/docs/tree/main/media/react_react_native_components.png)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## React Native Components are based on React
![React and React Native Components](https://github.com/Codecademy/docs/tree/main/media/react_react_native_components.png)
![React and React Native Components](https://github.com/Codecademy/docs/tree/main/media/react_react_native_components.png)


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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
### Examples
React Natives uses the same component syntax structure for its views to display elements to the screen, like in React.js. The following examples are of a `Box` component defined as both a class and functional component:


### Function Component:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Function Component:

```jsx
import React from 'react';
import { Text } from 'react-native';

const Box= () => {
return (
<Text>I have a small box</Text>
);
}

export default Box;
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
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>
);
}
}
export default Box;

```

### Class Component:

```jsx
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;
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Class Component:
```jsx
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;
```

I would replace this with the following note:

> **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.

React Native Components also use JSX, accept Props and manage State.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
React Native Components also use JSX, accept Props and manage State.
Views 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 writing elements inside JavaScript, and also the use of variables inside them:

```jsx
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;
yangc95 marked this conversation as resolved.
Show resolved Hide resolved
```

### Props

Most React Native Core Components accept Props, for example, you can pass different sizes via Props to this Box component:
ericsonrd marked this conversation as resolved.
Show resolved Hide resolved

```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>
);
}

export default BoxCollection;
ericsonrd marked this conversation as resolved.
Show resolved Hide resolved
```

### State

Just like React, React Native Components use State to handle data that changes over time, such as with user interaction:
ericsonrd marked this conversation as resolved.
Show resolved Hide resolved

```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>
<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;
ericsonrd marked this conversation as resolved.
Show resolved Hide resolved
```
Binary file added media/react_native_UI_Views.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/react_react_native_components.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.