You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/intro-react.md
+4-18Lines changed: 4 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,6 @@ If you want to dig deeper, we encourage you to check out [React’s official doc
22
22
The rest of this introduction to React uses cats in its examples: friendly, approachable creatures that need names and a cafe to work in. Here is your very first Cat component:
23
23
24
24
```SnackPlayer name=Your%20Cat
25
-
import React from 'react';
26
25
import {Text} from 'react-native';
27
26
28
27
const Cat = () => {
@@ -32,10 +31,9 @@ const Cat = () => {
32
31
export default Cat;
33
32
```
34
33
35
-
Here is how you do it: To define your `Cat` component, first use JavaScript’s [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) to import React and React Native’s [`Text`](/docs/next/text) Core Component:
34
+
Here is how you do it: To define your `Cat` component, first use JavaScript’s [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) to import React Native’s [`Text`](/docs/next/text) Core Component:
36
35
37
36
```tsx
38
-
importReactfrom'react';
39
37
import {Text} from'react-native';
40
38
```
41
39
@@ -76,7 +74,6 @@ Now take a closer look at that `return` statement. `<Text>Hello, I am your cat!<
76
74
React and React Native use **JSX,** a syntax that lets you write elements inside JavaScript like so: `<Text>Hello, I am your cat!</Text>`. The React docs have [a comprehensive guide to JSX](https://react.dev/learn/writing-markup-with-jsx) you can refer to learn even more. Because JSX is JavaScript, you can use variables inside it. Here you are declaring a name for the cat, `name`, and embedding it with curly braces inside `<Text>`.
77
75
78
76
```SnackPlayer name=Curly%20Braces
79
-
import React from 'react';
80
77
import {Text} from 'react-native';
81
78
82
79
const Cat = () => {
@@ -93,7 +90,6 @@ Any JavaScript expression will work between curly braces, including function cal
You can think of curly braces as creating a portal into JS functionality in your JSX!
136
131
137
-
:::tip
138
-
Because JSX is included in the React library, it won’t work if you don’t have `import React from 'react'` at the top of your file!
139
-
:::
140
-
141
132
## Custom Components
142
133
143
134
You’ve already met [React Native’s Core Components](intro-react-native-components). React lets you nest these components inside each other to create new components. These nestable, reusable components are at the heart of the React paradigm.
144
135
145
136
For example, you can nest [`Text`](text) and [`TextInput`](textinput) inside a [`View`](view) below, and React Native will render them together:
146
137
147
138
```SnackPlayer name=Custom%20Components
148
-
import React from 'react';
149
139
import {Text, TextInput, View} from 'react-native';
150
140
151
141
const Cat = () => {
@@ -190,7 +180,6 @@ On Android, you usually put your views inside `LinearLayout`, `FrameLayout`, `Re
190
180
You can render this component multiple times and in multiple places without repeating your code by using `<Cat>`:
191
181
192
182
```SnackPlayer name=Multiple%20Components
193
-
import React from 'react';
194
183
import {Text, View} from 'react-native';
195
184
196
185
const Cat = () => {
@@ -227,7 +216,6 @@ You can put as many cats in your cafe as you like. Each `<Cat>` renders a unique
227
216
<TabItemvalue="javascript">
228
217
229
218
```SnackPlayer name=Multiple%20Props&ext=js
230
-
import React from 'react';
231
219
import {Text, View} from 'react-native';
232
220
233
221
const Cat = props => {
@@ -255,7 +243,6 @@ export default Cafe;
255
243
<TabItemvalue="typescript">
256
244
257
245
```SnackPlayer name=Multiple%20Props&ext=tsx
258
-
import React from 'react';
259
246
import {Text, View} from 'react-native';
260
247
261
248
type CatProps = {
@@ -289,7 +276,6 @@ export default Cafe;
289
276
Most of React Native’s Core Components can be customized with props, too. For example, when using [`Image`](image), you pass it a prop named [`source`](image#source) to define what image it shows:
290
277
291
278
```SnackPlayer name=Props
292
-
import React from 'react';
293
279
import {Text, View, Image} from 'react-native';
294
280
295
281
const CatApp = () => {
@@ -333,7 +319,7 @@ You can add state to a component by calling [React’s `useState` Hook](https://
333
319
<TabItemvalue="javascript">
334
320
335
321
```SnackPlayer name=State&ext=js
336
-
import React, {useState} from 'react';
322
+
import {useState} from 'react';
337
323
import {Button, Text, View} from 'react-native';
338
324
339
325
const Cat = props => {
@@ -371,7 +357,7 @@ export default Cafe;
371
357
<TabItemvalue="typescript">
372
358
373
359
```SnackPlayer name=State&ext=tsx
374
-
import React, {useState} from 'react';
360
+
import {useState} from 'react';
375
361
import {Button, Text, View} from 'react-native';
376
362
377
363
type CatProps = {
@@ -415,7 +401,7 @@ export default Cafe;
415
401
First, you will want to import `useState` from React like so:
416
402
417
403
```tsx
418
-
importReact, {useState} from'react';
404
+
import {useState} from'react';
419
405
```
420
406
421
407
Then you declare the component’s state by calling `useState` inside its function. In this example, `useState` creates an `isHungry` state variable:
Copy file name to clipboardExpand all lines: website/versioned_docs/version-0.77/intro-react.md
+29-24Lines changed: 29 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,6 @@ If you want to dig deeper, we encourage you to check out [React’s official doc
22
22
The rest of this introduction to React uses cats in its examples: friendly, approachable creatures that need names and a cafe to work in. Here is your very first Cat component:
23
23
24
24
```SnackPlayer name=Your%20Cat
25
-
import React from 'react';
26
25
import {Text} from 'react-native';
27
26
28
27
const Cat = () => {
@@ -32,10 +31,9 @@ const Cat = () => {
32
31
export default Cat;
33
32
```
34
33
35
-
Here is how you do it: To define your `Cat` component, first use JavaScript’s [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) to import React and React Native’s [`Text`](/docs/next/text) Core Component:
34
+
Here is how you do it: To define your `Cat` component, first use JavaScript’s [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) to import React Native’s [`Text`](/docs/next/text) Core Component:
36
35
37
36
```tsx
38
-
importReactfrom'react';
39
37
import {Text} from'react-native';
40
38
```
41
39
@@ -65,7 +63,9 @@ const Cat = () => {
65
63
exportdefaultCat;
66
64
```
67
65
68
-
> This is one of many ways to export your component. This kind of export works well with the Snack Player. However, depending on your app’s file structure, you might need to use a different convention. This [handy cheatsheet on JavaScript imports and exports](https://medium.com/dailyjs/javascript-module-cheatsheet-7bd474f1d829) can help.
66
+
:::tip
67
+
This is one of many ways to export your component. This kind of export works well with the Snack Player. However, depending on your app’s file structure, you might need to use a different convention. This [handy cheatsheet on JavaScript imports and exports](https://medium.com/dailyjs/javascript-module-cheatsheet-7bd474f1d829) can help.
68
+
:::
69
69
70
70
Now take a closer look at that `return` statement. `<Text>Hello, I am your cat!</Text>` is using a kind of JavaScript syntax that makes writing elements convenient: JSX.
71
71
@@ -74,7 +74,6 @@ Now take a closer look at that `return` statement. `<Text>Hello, I am your cat!<
74
74
React and React Native use **JSX,** a syntax that lets you write elements inside JavaScript like so: `<Text>Hello, I am your cat!</Text>`. The React docs have [a comprehensive guide to JSX](https://react.dev/learn/writing-markup-with-jsx) you can refer to learn even more. Because JSX is JavaScript, you can use variables inside it. Here you are declaring a name for the cat, `name`, and embedding it with curly braces inside `<Text>`.
75
75
76
76
```SnackPlayer name=Curly%20Braces
77
-
import React from 'react';
78
77
import {Text} from 'react-native';
79
78
80
79
const Cat = () => {
@@ -91,7 +90,6 @@ Any JavaScript expression will work between curly braces, including function cal
You can think of curly braces as creating a portal into JS functionality in your JSX!
134
131
135
-
> Because JSX is included in the React library, it won’t work if you don’t have `import React from 'react'` at the top of your file!
136
-
137
132
## Custom Components
138
133
139
134
You’ve already met [React Native’s Core Components](intro-react-native-components). React lets you nest these components inside each other to create new components. These nestable, reusable components are at the heart of the React paradigm.
140
135
141
136
For example, you can nest [`Text`](text) and [`TextInput`](textinput) inside a [`View`](view) below, and React Native will render them together:
142
137
143
138
```SnackPlayer name=Custom%20Components
144
-
import React from 'react';
145
139
import {Text, TextInput, View} from 'react-native';
146
140
147
141
const Cat = () => {
@@ -169,20 +163,23 @@ export default Cat;
169
163
170
164
<TabItemvalue="web">
171
165
172
-
> If you’re familiar with web development, `<View>` and `<Text>` might remind you of HTML! You can think of them as the `<div>` and `<p>` tags of application development.
166
+
:::info
167
+
If you’re familiar with web development, `<View>` and `<Text>` might remind you of HTML! You can think of them as the `<div>` and `<p>` tags of application development.
168
+
:::
173
169
174
170
</TabItem>
175
171
<TabItemvalue="android">
176
172
177
-
> On Android, you usually put your views inside `LinearLayout`, `FrameLayout`, `RelativeLayout`, etc. to define how the view’s children will be arranged on the screen. In React Native, `View` uses Flexbox for its children’s layout. You can learn more in [our guide to layout with Flexbox](flexbox).
173
+
:::info
174
+
On Android, you usually put your views inside `LinearLayout`, `FrameLayout`, `RelativeLayout`, etc. to define how the view’s children will be arranged on the screen. In React Native, `View` uses Flexbox for its children’s layout. You can learn more in [our guide to layout with Flexbox](flexbox).
175
+
:::
178
176
179
177
</TabItem>
180
178
</Tabs>
181
179
182
180
You can render this component multiple times and in multiple places without repeating your code by using `<Cat>`:
183
181
184
182
```SnackPlayer name=Multiple%20Components
185
-
import React from 'react';
186
183
import {Text, View} from 'react-native';
187
184
188
185
const Cat = () => {
@@ -219,7 +216,6 @@ You can put as many cats in your cafe as you like. Each `<Cat>` renders a unique
219
216
<TabItemvalue="javascript">
220
217
221
218
```SnackPlayer name=Multiple%20Props&ext=js
222
-
import React from 'react';
223
219
import {Text, View} from 'react-native';
224
220
225
221
const Cat = props => {
@@ -247,7 +243,6 @@ export default Cafe;
247
243
<TabItemvalue="typescript">
248
244
249
245
```SnackPlayer name=Multiple%20Props&ext=tsx
250
-
import React from 'react';
251
246
import {Text, View} from 'react-native';
252
247
253
248
type CatProps = {
@@ -281,7 +276,6 @@ export default Cafe;
281
276
Most of React Native’s Core Components can be customized with props, too. For example, when using [`Image`](image), you pass it a prop named [`source`](image#source) to define what image it shows:
282
277
283
278
```SnackPlayer name=Props
284
-
import React from 'react';
285
279
import {Text, View, Image} from 'react-native';
286
280
287
281
const CatApp = () => {
@@ -303,15 +297,19 @@ export default CatApp;
303
297
304
298
`Image` has [many different props](image#props), including [`style`](image#style), which accepts a JS object of design and layout related property-value pairs.
305
299
306
-
> Notice the double curly braces `{{ }}` surrounding `style`‘s width and height. In JSX, JavaScript values are referenced with `{}`. This is handy if you are passing something other than a string as props, like an array or number: `<Cat food={["fish", "kibble"]} age={2} />`. However, JS objects are **_also_** denoted with curly braces: `{width: 200, height: 200}`. Therefore, to pass a JS object in JSX, you must wrap the object in **another pair** of curly braces: `{{width: 200, height: 200}}`
300
+
:::note
301
+
Notice the double curly braces `{{ }}` surrounding `style`‘s width and height. In JSX, JavaScript values are referenced with `{}`. This is handy if you are passing something other than a string as props, like an array or number: `<Cat food={["fish", "kibble"]} age={2} />`. However, JS objects are **_also_** denoted with curly braces: `{width: 200, height: 200}`. Therefore, to pass a JS object in JSX, you must wrap the object in **another pair** of curly braces: `{{width: 200, height: 200}}`
302
+
:::
307
303
308
304
You can build many things with props and the Core Components [`Text`](text), [`Image`](image), and [`View`](view)! But to build something interactive, you’ll need state.
309
305
310
306
## State
311
307
312
308
While you can think of props as arguments you use to configure how components render, **state** is like a component’s personal data storage. State is useful for handling data that changes over time or that comes from user interaction. State gives your components memory!
313
309
314
-
> As a general rule, use props to configure a component when it renders. Use state to keep track of any component data that you expect to change over time.
310
+
:::info
311
+
As a general rule, use props to configure a component when it renders. Use state to keep track of any component data that you expect to change over time.
312
+
:::
315
313
316
314
The following example takes place in a cat cafe where two hungry cats are waiting to be fed. Their hunger, which we expect to change over time (unlike their names), is stored as state. To feed the cats, press their buttons—which will update their state.
317
315
@@ -321,7 +319,7 @@ You can add state to a component by calling [React’s `useState` Hook](https://
321
319
<TabItemvalue="javascript">
322
320
323
321
```SnackPlayer name=State&ext=js
324
-
import React, {useState} from 'react';
322
+
import {useState} from 'react';
325
323
import {Button, Text, View} from 'react-native';
326
324
327
325
const Cat = props => {
@@ -359,7 +357,7 @@ export default Cafe;
359
357
<TabItemvalue="typescript">
360
358
361
359
```SnackPlayer name=State&ext=tsx
362
-
import React, {useState} from 'react';
360
+
import {useState} from 'react';
363
361
import {Button, Text, View} from 'react-native';
364
362
365
363
type CatProps = {
@@ -403,7 +401,7 @@ export default Cafe;
403
401
First, you will want to import `useState` from React like so:
404
402
405
403
```tsx
406
-
importReact, {useState} from'react';
404
+
import {useState} from'react';
407
405
```
408
406
409
407
Then you declare the component’s state by calling `useState` inside its function. In this example, `useState` creates an `isHungry` state variable:
> 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)`!
416
+
:::tip
417
+
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)`!
418
+
:::
419
419
420
420
Calling `useState` does two things:
421
421
@@ -445,7 +445,10 @@ Now, when someone presses the button, `onPress` will fire, calling the `setIsHun
445
445
/>
446
446
```
447
447
448
-
> You might’ve noticed that although `isHungry` is a [const](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/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`.
448
+
:::info
449
+
You might’ve noticed that although `isHungry` is a [const](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/const), it is seemingly reassignable! The `const` keyword here does not mean that the state itself is immutable. Rather, it means that the reference to the object, that contains the state and the function to update it, will not change.
450
+
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`.
451
+
:::
449
452
450
453
Finally, put your cats inside a `Cafe` component:
451
454
@@ -460,7 +463,9 @@ const Cafe = () => {
460
463
};
461
464
```
462
465
463
-
> See the `<>` and `</>` above? These bits of JSX are [fragments](https://react.dev/reference/react/Fragment). Adjacent JSX elements must be wrapped in an enclosing tag. Fragments let you do that without nesting an extra, unnecessary wrapping element like `View`.
466
+
:::info
467
+
See the `<>` and `</>` above? These bits of JSX are [fragments](https://react.dev/reference/react/Fragment). Adjacent JSX elements must be wrapped in an enclosing tag. Fragments let you do that without nesting an extra, unnecessary wrapping element like `View`.
0 commit comments