Skip to content

Commit e33433e

Browse files
authored
remove outdated React import information from intro (#5100)
* remove oudated React import information from intro * few more corrections
1 parent 929a75a commit e33433e

11 files changed

Lines changed: 150 additions & 225 deletions

File tree

docs/intro-react.md

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ If you want to dig deeper, we encourage you to check out [React’s official doc
2222
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:
2323

2424
```SnackPlayer name=Your%20Cat
25-
import React from 'react';
2625
import {Text} from 'react-native';
2726
2827
const Cat = () => {
@@ -32,10 +31,9 @@ const Cat = () => {
3231
export default Cat;
3332
```
3433

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:
3635

3736
```tsx
38-
import React from 'react';
3937
import {Text} from 'react-native';
4038
```
4139

@@ -76,7 +74,6 @@ Now take a closer look at that `return` statement. `<Text>Hello, I am your cat!<
7674
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>`.
7775

7876
```SnackPlayer name=Curly%20Braces
79-
import React from 'react';
8077
import {Text} from 'react-native';
8178
8279
const Cat = () => {
@@ -93,7 +90,6 @@ Any JavaScript expression will work between curly braces, including function cal
9390
<TabItem value="javascript">
9491

9592
```SnackPlayer name=Curly%20Braces&ext=js
96-
import React from 'react';
9793
import {Text} from 'react-native';
9894
9995
const getFullName = (firstName, secondName, thirdName) => {
@@ -111,7 +107,6 @@ export default Cat;
111107
<TabItem value="typescript">
112108

113109
```SnackPlayer name=Curly%20Braces&ext=tsx
114-
import React from 'react';
115110
import {Text} from 'react-native';
116111
117112
const getFullName = (
@@ -134,18 +129,13 @@ export default Cat;
134129

135130
You can think of curly braces as creating a portal into JS functionality in your JSX!
136131

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-
141132
## Custom Components
142133

143134
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.
144135

145136
For example, you can nest [`Text`](text) and [`TextInput`](textinput) inside a [`View`](view) below, and React Native will render them together:
146137

147138
```SnackPlayer name=Custom%20Components
148-
import React from 'react';
149139
import {Text, TextInput, View} from 'react-native';
150140
151141
const Cat = () => {
@@ -190,7 +180,6 @@ On Android, you usually put your views inside `LinearLayout`, `FrameLayout`, `Re
190180
You can render this component multiple times and in multiple places without repeating your code by using `<Cat>`:
191181

192182
```SnackPlayer name=Multiple%20Components
193-
import React from 'react';
194183
import {Text, View} from 'react-native';
195184
196185
const Cat = () => {
@@ -227,7 +216,6 @@ You can put as many cats in your cafe as you like. Each `<Cat>` renders a unique
227216
<TabItem value="javascript">
228217

229218
```SnackPlayer name=Multiple%20Props&ext=js
230-
import React from 'react';
231219
import {Text, View} from 'react-native';
232220
233221
const Cat = props => {
@@ -255,7 +243,6 @@ export default Cafe;
255243
<TabItem value="typescript">
256244

257245
```SnackPlayer name=Multiple%20Props&ext=tsx
258-
import React from 'react';
259246
import {Text, View} from 'react-native';
260247
261248
type CatProps = {
@@ -289,7 +276,6 @@ export default Cafe;
289276
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:
290277

291278
```SnackPlayer name=Props
292-
import React from 'react';
293279
import {Text, View, Image} from 'react-native';
294280
295281
const CatApp = () => {
@@ -333,7 +319,7 @@ You can add state to a component by calling [React’s `useState` Hook](https://
333319
<TabItem value="javascript">
334320

335321
```SnackPlayer name=State&ext=js
336-
import React, {useState} from 'react';
322+
import {useState} from 'react';
337323
import {Button, Text, View} from 'react-native';
338324
339325
const Cat = props => {
@@ -371,7 +357,7 @@ export default Cafe;
371357
<TabItem value="typescript">
372358

373359
```SnackPlayer name=State&ext=tsx
374-
import React, {useState} from 'react';
360+
import {useState} from 'react';
375361
import {Button, Text, View} from 'react-native';
376362
377363
type CatProps = {
@@ -415,7 +401,7 @@ export default Cafe;
415401
First, you will want to import `useState` from React like so:
416402

417403
```tsx
418-
import React, {useState} from 'react';
404+
import {useState} from 'react';
419405
```
420406

421407
Then you declare the component’s state by calling `useState` inside its function. In this example, `useState` creates an `isHungry` state variable:

website/versioned_docs/version-0.77/intro-react.md

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ If you want to dig deeper, we encourage you to check out [React’s official doc
2222
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:
2323

2424
```SnackPlayer name=Your%20Cat
25-
import React from 'react';
2625
import {Text} from 'react-native';
2726
2827
const Cat = () => {
@@ -32,10 +31,9 @@ const Cat = () => {
3231
export default Cat;
3332
```
3433

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:
3635

3736
```tsx
38-
import React from 'react';
3937
import {Text} from 'react-native';
4038
```
4139

@@ -65,7 +63,9 @@ const Cat = () => {
6563
export default Cat;
6664
```
6765

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+
:::
6969

7070
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.
7171

@@ -74,7 +74,6 @@ Now take a closer look at that `return` statement. `<Text>Hello, I am your cat!<
7474
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>`.
7575

7676
```SnackPlayer name=Curly%20Braces
77-
import React from 'react';
7877
import {Text} from 'react-native';
7978
8079
const Cat = () => {
@@ -91,7 +90,6 @@ Any JavaScript expression will work between curly braces, including function cal
9190
<TabItem value="javascript">
9291

9392
```SnackPlayer name=Curly%20Braces&ext=js
94-
import React from 'react';
9593
import {Text} from 'react-native';
9694
9795
const getFullName = (firstName, secondName, thirdName) => {
@@ -109,7 +107,6 @@ export default Cat;
109107
<TabItem value="typescript">
110108

111109
```SnackPlayer name=Curly%20Braces&ext=tsx
112-
import React from 'react';
113110
import {Text} from 'react-native';
114111
115112
const getFullName = (
@@ -132,16 +129,13 @@ export default Cat;
132129

133130
You can think of curly braces as creating a portal into JS functionality in your JSX!
134131

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-
137132
## Custom Components
138133

139134
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.
140135

141136
For example, you can nest [`Text`](text) and [`TextInput`](textinput) inside a [`View`](view) below, and React Native will render them together:
142137

143138
```SnackPlayer name=Custom%20Components
144-
import React from 'react';
145139
import {Text, TextInput, View} from 'react-native';
146140
147141
const Cat = () => {
@@ -169,20 +163,23 @@ export default Cat;
169163

170164
<TabItem value="web">
171165

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+
:::
173169

174170
</TabItem>
175171
<TabItem value="android">
176172

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+
:::
178176

179177
</TabItem>
180178
</Tabs>
181179

182180
You can render this component multiple times and in multiple places without repeating your code by using `<Cat>`:
183181

184182
```SnackPlayer name=Multiple%20Components
185-
import React from 'react';
186183
import {Text, View} from 'react-native';
187184
188185
const Cat = () => {
@@ -219,7 +216,6 @@ You can put as many cats in your cafe as you like. Each `<Cat>` renders a unique
219216
<TabItem value="javascript">
220217

221218
```SnackPlayer name=Multiple%20Props&ext=js
222-
import React from 'react';
223219
import {Text, View} from 'react-native';
224220
225221
const Cat = props => {
@@ -247,7 +243,6 @@ export default Cafe;
247243
<TabItem value="typescript">
248244

249245
```SnackPlayer name=Multiple%20Props&ext=tsx
250-
import React from 'react';
251246
import {Text, View} from 'react-native';
252247
253248
type CatProps = {
@@ -281,7 +276,6 @@ export default Cafe;
281276
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:
282277

283278
```SnackPlayer name=Props
284-
import React from 'react';
285279
import {Text, View, Image} from 'react-native';
286280
287281
const CatApp = () => {
@@ -303,15 +297,19 @@ export default CatApp;
303297

304298
`Image` has [many different props](image#props), including [`style`](image#style), which accepts a JS object of design and layout related property-value pairs.
305299

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+
:::
307303

308304
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.
309305

310306
## State
311307

312308
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!
313309

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+
:::
315313

316314
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.
317315

@@ -321,7 +319,7 @@ You can add state to a component by calling [React’s `useState` Hook](https://
321319
<TabItem value="javascript">
322320

323321
```SnackPlayer name=State&ext=js
324-
import React, {useState} from 'react';
322+
import {useState} from 'react';
325323
import {Button, Text, View} from 'react-native';
326324
327325
const Cat = props => {
@@ -359,7 +357,7 @@ export default Cafe;
359357
<TabItem value="typescript">
360358

361359
```SnackPlayer name=State&ext=tsx
362-
import React, {useState} from 'react';
360+
import {useState} from 'react';
363361
import {Button, Text, View} from 'react-native';
364362
365363
type CatProps = {
@@ -403,7 +401,7 @@ export default Cafe;
403401
First, you will want to import `useState` from React like so:
404402

405403
```tsx
406-
import React, {useState} from 'react';
404+
import {useState} from 'react';
407405
```
408406

409407
Then you declare the component’s state by calling `useState` inside its function. In this example, `useState` creates an `isHungry` state variable:
@@ -415,7 +413,9 @@ const Cat = (props: CatProps) => {
415413
};
416414
```
417415

418-
> 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+
:::
419419

420420
Calling `useState` does two things:
421421

@@ -445,7 +445,10 @@ Now, when someone presses the button, `onPress` will fire, calling the `setIsHun
445445
/>
446446
```
447447

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+
:::
449452

450453
Finally, put your cats inside a `Cafe` component:
451454

@@ -460,7 +463,9 @@ const Cafe = () => {
460463
};
461464
```
462465

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`.
468+
:::
464469

465470
---
466471

0 commit comments

Comments
 (0)