import
{ Text
} from 'react-native';
Your component starts as a function:
-
function Cat() {}
+const Cat = () => {};
You can think of components as blueprints. Whatever a function component returns is rendered as a React element. React elements let you describe what you want to see on the screen.
Here the Cat component will render a <Text> element:
-function Cat() {
+const Cat = () => {
return <Text>Hello, I am your cat!</Text>;
-}
+};
+
+export default Cat;
You can export your function component with JavaScript’s export default for use throughout your app like so:
-export default function Cat() {
+const Cat = () => {
return <Text>Hello, I am your cat!</Text>;
-}
+};
+
+export default Cat;
Class components tend to be a bit more verbose than function components.
import React, { Component } from 'react';
import { Text } from 'react-native';
-export default class Cat extends Component {
+class Cat extends Component {
render() {
return (
<Text>Hello, I am your cat!</Text>
);
}
}
+
+export default Cat;
}
And as with function components, you can export your class component:
-
export default class Cat extends Component {
+class Cat extends Component {
render() {
return <Text>Hello, I am your cat!</Text>;
}
}
+
+export default Cat;
@@ -188,16 +198,18 @@
import React from 'react';
import { Text } from 'react-native';
-export default function Cat() {
+const Cat = () => {
const name = "Maru";
return (
<Text>Hello, I am {name}!</Text>
);
}
+
+export default Cat;
import React from 'react';
import { Text } from 'react-native';
-export default function Cat() {
- function getFullName(firstName, secondName, thirdName) {
- return firstName + " " + secondName + " " + thirdName;
- }
+const getFullName = (firstName, secondName, thirdName) => {
+ return firstName + " " + secondName + " " + thirdName;
+}
+const Cat = () => {
return (
<Text>
Hello, I am {getFullName("Rum", "Tum", "Tugger")}!
</Text>
);
}
+
+export default Cat;
import React from 'react';
import { Text, TextInput, View } from 'react-native';
-export default function Cat() {
+const Cat = () => {
return (
<View>
<Text>Hello, I am...</Text>
@@ -264,10 +278,12 @@
</View>
);
}
+
+export default Cat;
import React from 'react';
import { Text, TextInput, View } from 'react-native';
-function Cat() {
+const Cat = () => {
return (
<View>
<Text>I am also a cat!</Text>
@@ -307,7 +323,7 @@
);
}
-export default function Cafe() {
+const Cafe = () => {
return (
<View>
<Text>Welcome!</Text>
@@ -317,10 +333,12 @@
</View>
);
}
+
+export default Cafe;
import React from 'react';
import { Text, View } from 'react-native';
-function Cat(props) {
+const Cat = (props) => {
return (
<View>
<Text>Hello, I am {props.name}!</Text>
@@ -347,7 +365,7 @@
);
}
-export default function Cafe() {
+const Cafe = () => {
return (
<View>
<Cat name="Maru" />
@@ -356,10 +374,12 @@
</View>
);
}
+
+export default Cafe;
import React from 'react';
import { Text, View, Image } from 'react-native';
-export default function CatApp() {
+const CatApp = () => {
return (
<View>
<Image
@@ -386,10 +406,12 @@
</View>
);
}
+
+export default CatApp;
import React, { useState } from "react";
import { Button, Text, View } from "react-native";
-function Cat(props) {
+const Cat = (props) => {
const [isHungry, setIsHungry] = useState(true);
return (
@@ -446,7 +468,7 @@
);
}
-export default function Cafe() {
+const Cafe = () => {
return (
<>
<Cat name="Munkustrap" />
@@ -454,10 +476,12 @@ export default function Cafe() {
</>
);
}
+
+export default Cafe;
import React, { useState } from 'react';
Then you declare the component’s state by calling useState inside its function. In this example, useState creates an isHungry state variable:
-
function Cat(props) {
+const Cat = (props) => {
const [isHungry, setIsHungry] = useState(true);
-}
+};
You can use useState to track any kind of data: strings, numbers, Booleans, arrays, objects. For example, you can track the number of times a cat has been petted with const [timesPetted, setTimesPetted] = useState(0)!
@@ -506,21 +530,21 @@ export default function Cafe() {
You might’ve noticed that although isHungry is a const, it is seemingly reassignable! What is happening is when a state-setting function like setIsHungry is called, its component will re-render. In this case the Cat function will run again—and this time, useState will give us the next value of isHungry.
Finally, put your cats inside a Cafe component:
-export default function Cafe() {
+const Cafe = () => {
return (
<>
<Cat name="Munkustrap" />
<Cat name="Spot" />
</>
);
-}
+};
The older class components approach is a little different when it comes to state.
import React, { Component } from "react";
import { Button, Text, View } from "react-native";
-export class Cat extends Component {
+class Cat extends Component {
state = { isHungry: true };
render(props) {
@@ -544,7 +568,7 @@ export default function Cafe() {
}
}
-export default class Cafe extends Component {
+class Cafe extends Component {
render() {
return (
<>
@@ -554,10 +578,12 @@ export default class Cafe extends Component {
);
}
}
+
+export default Cafe;
/
>
Finally, put your cats inside a Cafe component:
-
export default class Cafe extends Component {
+class Cafe extends Component {
render() {
return (
<>
@@ -617,6 +643,8 @@ export default class Cafe extends Component {
);
}
}
+
+export default Cafe;
diff --git a/docs/next/intro-react/index.html b/docs/next/intro-react/index.html
index 49a6020cc5e..3f9f2bd50ec 100644
--- a/docs/next/intro-react/index.html
+++ b/docs/next/intro-react/index.html
@@ -93,15 +93,17 @@
import React from 'react';
import { Text } from 'react-native';
-export default function Cat() {
+const Cat = () => {
return (
<Text>Hello, I am your cat!</Text>
);
}
+
+export default Cat;
import
{ Text
} from 'react-native';
Your component starts as a function:
-
function Cat() {}
+const Cat = () => {};
You can think of components as blueprints. Whatever a function component returns is rendered as a React element. React elements let you describe what you want to see on the screen.
Here the Cat component will render a <Text> element:
-function Cat() {
+const Cat = () => {
return <Text>Hello, I am your cat!</Text>;
-}
+};
+
+export default Cat;
You can export your function component with JavaScript’s export default for use throughout your app like so:
-export default function Cat() {
+const Cat = () => {
return <Text>Hello, I am your cat!</Text>;
-}
+};
+
+export default Cat;
Class components tend to be a bit more verbose than function components.
import React, { Component } from 'react';
import { Text } from 'react-native';
-export default class Cat extends Component {
+class Cat extends Component {
render() {
return (
<Text>Hello, I am your cat!</Text>
);
}
}
+
+export default Cat;
}
And as with function components, you can export your class component:
-
export default class Cat extends Component {
+class Cat extends Component {
render() {
return <Text>Hello, I am your cat!</Text>;
}
}
+
+export default Cat;
@@ -188,16 +198,18 @@
import React from 'react';
import { Text } from 'react-native';
-export default function Cat() {
+const Cat = () => {
const name = "Maru";
return (
<Text>Hello, I am {name}!</Text>
);
}
+
+export default Cat;
import React from 'react';
import { Text } from 'react-native';
-export default function Cat() {
- function getFullName(firstName, secondName, thirdName) {
- return firstName + " " + secondName + " " + thirdName;
- }
+const getFullName = (firstName, secondName, thirdName) => {
+ return firstName + " " + secondName + " " + thirdName;
+}
+const Cat = () => {
return (
<Text>
Hello, I am {getFullName("Rum", "Tum", "Tugger")}!
</Text>
);
}
+
+export default Cat;
import React from 'react';
import { Text, TextInput, View } from 'react-native';
-export default function Cat() {
+const Cat = () => {
return (
<View>
<Text>Hello, I am...</Text>
@@ -264,10 +278,12 @@
</View>
);
}
+
+export default Cat;
import React from 'react';
import { Text, TextInput, View } from 'react-native';
-function Cat() {
+const Cat = () => {
return (
<View>
<Text>I am also a cat!</Text>
@@ -307,7 +323,7 @@
);
}
-export default function Cafe() {
+const Cafe = () => {
return (
<View>
<Text>Welcome!</Text>
@@ -317,10 +333,12 @@
</View>
);
}
+
+export default Cafe;
import React from 'react';
import { Text, View } from 'react-native';
-function Cat(props) {
+const Cat = (props) => {
return (
<View>
<Text>Hello, I am {props.name}!</Text>
@@ -347,7 +365,7 @@
);
}
-export default function Cafe() {
+const Cafe = () => {
return (
<View>
<Cat name="Maru" />
@@ -356,10 +374,12 @@
</View>
);
}
+
+export default Cafe;
import React from 'react';
import { Text, View, Image } from 'react-native';
-export default function CatApp() {
+const CatApp = () => {
return (
<View>
<Image
@@ -386,10 +406,12 @@
</View>
);
}
+
+export default CatApp;
import React, { useState } from "react";
import { Button, Text, View } from "react-native";
-function Cat(props) {
+const Cat = (props) => {
const [isHungry, setIsHungry] = useState(true);
return (
@@ -446,7 +468,7 @@
);
}
-export default function Cafe() {
+const Cafe = () => {
return (
<>
<Cat name="Munkustrap" />
@@ -454,10 +476,12 @@ export default function Cafe() {
</>
);
}
+
+export default Cafe;
import React, { useState } from 'react';
Then you declare the component’s state by calling useState inside its function. In this example, useState creates an isHungry state variable:
-
function Cat(props) {
+const Cat = (props) => {
const [isHungry, setIsHungry] = useState(true);
-}
+};
You can use useState to track any kind of data: strings, numbers, Booleans, arrays, objects. For example, you can track the number of times a cat has been petted with const [timesPetted, setTimesPetted] = useState(0)!
@@ -506,21 +530,21 @@ export default function Cafe() {
You might’ve noticed that although isHungry is a const, it is seemingly reassignable! What is happening is when a state-setting function like setIsHungry is called, its component will re-render. In this case the Cat function will run again—and this time, useState will give us the next value of isHungry.
Finally, put your cats inside a Cafe component:
-export default function Cafe() {
+const Cafe = () => {
return (
<>
<Cat name="Munkustrap" />
<Cat name="Spot" />
</>
);
-}
+};
The older class components approach is a little different when it comes to state.
import React, { Component } from "react";
import { Button, Text, View } from "react-native";
-export class Cat extends Component {
+class Cat extends Component {
state = { isHungry: true };
render(props) {
@@ -544,7 +568,7 @@ export default function Cafe() {
}
}
-export default class Cafe extends Component {
+class Cafe extends Component {
render() {
return (
<>
@@ -554,10 +578,12 @@ export default class Cafe extends Component {
);
}
}
+
+export default Cafe;
/
>
Finally, put your cats inside a Cafe component:
-
export default class Cafe extends Component {
+class Cafe extends Component {
render() {
return (
<>
@@ -617,6 +643,8 @@ export default class Cafe extends Component {
);
}
}
+
+export default Cafe;
diff --git a/docs/next/using-a-listview.html b/docs/next/using-a-listview.html
index 23b13c26783..411512afa8f 100644
--- a/docs/next/using-a-listview.html
+++ b/docs/next/using-a-listview.html
@@ -75,43 +75,45 @@
import React from 'react';
import { FlatList, StyleSheet, Text, View } from 'react-native';
-export default FlatListBasics = () => {
- return (
- <View style={styles.container}>
- <FlatList
- data={[
- {key: 'Devin'},
- {key: 'Dan'},
- {key: 'Dominic'},
- {key: 'Jackson'},
- {key: 'James'},
- {key: 'Joel'},
- {key: 'John'},
- {key: 'Jillian'},
- {key: 'Jimmy'},
- {key: 'Julie'},
- ]}
- renderItem={({item}) => <Text style={styles.item}>{item.key}</Text>}
- />
- </View>
- );
+const styles = StyleSheet.create({
+ container: {
+ flex: 1,
+ paddingTop: 22
+ },
+ item: {
+ padding: 10,
+ fontSize: 18,
+ height: 44,
+ },
+});
+
+const FlatListBasics = () => {
+ return (
+ <View style={styles.container}>
+ <FlatList
+ data={[
+ {key: 'Devin'},
+ {key: 'Dan'},
+ {key: 'Dominic'},
+ {key: 'Jackson'},
+ {key: 'James'},
+ {key: 'Joel'},
+ {key: 'John'},
+ {key: 'Jillian'},
+ {key: 'Jimmy'},
+ {key: 'Julie'},
+ ]}
+ renderItem={({item}) => <Text style={styles.item}>{item.key}</Text>}
+ />
+ </View>
+ );
}
-const styles = StyleSheet.create({
- container: {
- flex: 1,
- paddingTop: 22
- },
- item: {
- padding: 10,
- fontSize: 18,
- height: 44,
- },
-})
+export default FlatListBasics;
import React from 'react';
import { SectionList, StyleSheet, Text, View } from 'react-native';
-export default SectionListBasics = () => {
+const styles = StyleSheet.create({
+ container: {
+ flex: 1,
+ paddingTop: 22
+ },
+ sectionHeader: {
+ paddingTop: 2,
+ paddingLeft: 10,
+ paddingRight: 10,
+ paddingBottom: 2,
+ fontSize: 14,
+ fontWeight: 'bold',
+ backgroundColor: 'rgba(247,247,247,1.0)',
+ },
+ item: {
+ padding: 10,
+ fontSize: 18,
+ height: 44,
+ },
+})
+
+const SectionListBasics = () => {
return (
<View style={styles.container}>
<SectionList
@@ -143,30 +166,11 @@ const styles = StyleSheet.create({
);
}
-const styles = StyleSheet.create({
- container: {
- flex: 1,
- paddingTop: 22
- },
- sectionHeader: {
- paddingTop: 2,
- paddingLeft: 10,
- paddingRight: 10,
- paddingBottom: 2,
- fontSize: 14,
- fontWeight: 'bold',
- backgroundColor: 'rgba(247,247,247,1.0)',
- },
- item: {
- padding: 10,
- fontSize: 18,
- height: 44,
- },
-})
+export default SectionListBasics;
import React from 'react';
import { FlatList, StyleSheet, Text, View } from 'react-native';
-export default FlatListBasics = () => {
- return (
- <View style={styles.container}>
- <FlatList
- data={[
- {key: 'Devin'},
- {key: 'Dan'},
- {key: 'Dominic'},
- {key: 'Jackson'},
- {key: 'James'},
- {key: 'Joel'},
- {key: 'John'},
- {key: 'Jillian'},
- {key: 'Jimmy'},
- {key: 'Julie'},
- ]}
- renderItem={({item}) => <Text style={styles.item}>{item.key}</Text>}
- />
- </View>
- );
+const styles = StyleSheet.create({
+ container: {
+ flex: 1,
+ paddingTop: 22
+ },
+ item: {
+ padding: 10,
+ fontSize: 18,
+ height: 44,
+ },
+});
+
+const FlatListBasics = () => {
+ return (
+ <View style={styles.container}>
+ <FlatList
+ data={[
+ {key: 'Devin'},
+ {key: 'Dan'},
+ {key: 'Dominic'},
+ {key: 'Jackson'},
+ {key: 'James'},
+ {key: 'Joel'},
+ {key: 'John'},
+ {key: 'Jillian'},
+ {key: 'Jimmy'},
+ {key: 'Julie'},
+ ]}
+ renderItem={({item}) => <Text style={styles.item}>{item.key}</Text>}
+ />
+ </View>
+ );
}
-const styles = StyleSheet.create({
- container: {
- flex: 1,
- paddingTop: 22
- },
- item: {
- padding: 10,
- fontSize: 18,
- height: 44,
- },
-})
+export default FlatListBasics;
import React from 'react';
import { SectionList, StyleSheet, Text, View } from 'react-native';
-export default SectionListBasics = () => {
+const styles = StyleSheet.create({
+ container: {
+ flex: 1,
+ paddingTop: 22
+ },
+ sectionHeader: {
+ paddingTop: 2,
+ paddingLeft: 10,
+ paddingRight: 10,
+ paddingBottom: 2,
+ fontSize: 14,
+ fontWeight: 'bold',
+ backgroundColor: 'rgba(247,247,247,1.0)',
+ },
+ item: {
+ padding: 10,
+ fontSize: 18,
+ height: 44,
+ },
+})
+
+const SectionListBasics = () => {
return (
<View style={styles.container}>
<SectionList
@@ -143,30 +166,11 @@ const styles = StyleSheet.create({
);
}
-const styles = StyleSheet.create({
- container: {
- flex: 1,
- paddingTop: 22
- },
- sectionHeader: {
- paddingTop: 2,
- paddingLeft: 10,
- paddingRight: 10,
- paddingBottom: 2,
- fontSize: 14,
- fontWeight: 'bold',
- backgroundColor: 'rgba(247,247,247,1.0)',
- },
- item: {
- padding: 10,
- fontSize: 18,
- height: 44,
- },
-})
+export default SectionListBasics;