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
Now let's have a look at the files in the starter code.
303
303
304
-
#### `src/App.js` {/*appjs*/}
304
+
#### `App.js` {/*appjs*/}
305
305
306
-
The code in `src/App.js` creates a *component*. In React, a component is a piece of reusable code that represents a part of a user interface. Components are used to render, manage, and update the UI elements in your application. Let's look at the component line by line to see what's going on:
306
+
The code in `App.js` creates a _component_. In React, a component is a piece of reusable code that represents a part of a user interface. Components are used to render, manage, and update the UI elements in your application. Let's look at the component line by line to see what's going on:
307
307
308
308
```js {1}
309
309
exportdefaultfunctionSquare() {
@@ -321,13 +321,13 @@ export default function Square() {
321
321
322
322
The second line returns a button. The `return` JavaScript keyword means whatever comes after is returned as a value to the caller of the function. `<button>` is a *JSX element*. A JSX element is a combination of JavaScript code and HTML tags that describes what you'd like to display. `className="square"` is a button property or *prop* that tells CSS how to style the button. `X` is the text displayed inside of the button and `</button>` closes the JSX element to indicate that any following content shouldn't be placed inside the button.
323
323
324
-
#### `src/styles.css` {/*stylescss*/}
324
+
#### `styles.css` {/*stylescss*/}
325
325
326
-
Click on the file labeled `src/styles.css` in the *Files* section of CodeSandbox. This file defines the styles for your React app. The first two *CSS selectors* (`*` and `body`) define the style of large parts of your app while the `.square` selector defines the style of any component where the `className` property is set to `square`. In your code, that would match the button from your Square component in the `src/App.js` file.
326
+
Click on the file labeled `styles.css` in the _Files_ section of CodeSandbox. This file defines the styles for your React app. The first two _CSS selectors_ (`*` and `body`) define the style of large parts of your app while the `.square` selector defines the style of any component where the `className` property is set to `square`. In your code, that would match the button from your Square component in the `App.js` file.
327
327
328
-
#### `src/index.js` {/*indexjs*/}
328
+
#### `index.js` {/*indexjs*/}
329
329
330
-
Click on the file labeled `src/index.js` in the *Files* section of CodeSandbox. You won't be editing this file during the tutorial but it is the bridge between the component you created in the `src/App.js` file and the web browser.
330
+
Click on the file labeled `index.js` in the _Files_ section of CodeSandbox. You won't be editing this file during the tutorial but it is the bridge between the component you created in the `App.js` file and the web browser.
331
331
332
332
```jsx
333
333
import { StrictMode } from'react';
@@ -337,18 +337,18 @@ import './styles.css';
337
337
importAppfrom'./App';
338
338
```
339
339
340
-
Lines 1-5 bring all the necessary pieces together:
340
+
Lines 1-5 bring all the necessary pieces together:
341
341
342
-
- React
343
-
- React's library to talk to web browsers (React DOM)
344
-
- the styles for your components
345
-
- the component you created in `src/App.js`.
342
+
* React
343
+
* React's library to talk to web browsers (React DOM)
344
+
* the styles for your components
345
+
* the component you created in `App.js`.
346
346
347
347
The remainder of the file brings all the pieces together and injects the final product into `index.html` in the `public` folder.
348
348
349
349
### Building the board {/*building-the-board*/}
350
350
351
-
Let's get back to `src/App.js`. This is where you'll spend the rest of the tutorial.
351
+
Let's get back to `App.js`. This is where you'll spend the rest of the tutorial.
352
352
353
353
Currently the board is only a single square, but you need nine! If you just try and copy paste your square to make two squares like this:
354
354
@@ -389,7 +389,7 @@ Great! Now you just need to copy-paste a few times to add nine squares and...
389
389
390
390
Oh no! The squares are all in a single line, not in a grid like you need for our board. To fix this you'll need to group your squares into rows with `div`s and add some CSS classes. While you're at it, you'll give each square a number to make sure you know where each square is displayed.
391
391
392
-
In the `src/App.js` file, update the `Square` component to look like this:
392
+
In the `App.js` file, update the `Square` component to look like this:
393
393
394
394
```js {3-19}
395
395
exportdefaultfunctionSquare() {
@@ -415,7 +415,7 @@ export default function Square() {
415
415
}
416
416
```
417
417
418
-
The CSS defined in `src/styles.css` styles the divs with the `className` of `board-row`. Now that you've grouped your components into rows with the styled `div`s you have your tic-tac-toe board:
418
+
The CSS defined in `styles.css` styles the divs with the `className` of `board-row`. Now that you've grouped your components into rows with the styled `div`s you have your tic-tac-toe board:
419
419
420
420

421
421
@@ -551,7 +551,7 @@ export default function Board() {
551
551
}
552
552
```
553
553
554
-
Note how unlike the browser `div`s, your own components `Board` and `Square` must start with a capital letter.
554
+
Note how unlike the browser `div`s, your own components `Board` and `Square` must start with a capital letter.
555
555
556
556
Let's take a look:
557
557
@@ -723,7 +723,7 @@ function Square({ value }) {
723
723
}
724
724
```
725
725
726
-
If you click on a square now, you should see a log saying `"clicked!"` in the *Console* tab at the bottom of the *Browser* section in CodeSandbox. Clicking the square more than once will log `"clicked!"` again. Repeated console logs with the same message will not create more lines in the console. Instead, you will see an incrementing counter next to your first `"clicked!"` log.
726
+
If you click on a square now, you should see a log saying `"clicked!"` in the _Console_ tab at the bottom of the _Browser_ section in CodeSandbox. Clicking the square more than once will log `"clicked!"` again. Repeated console logs with the same message will not create more lines in the console. Instead, you will see an incrementing counter next to your first `"clicked!"` log.
React DevTools let you check the props and the state of your React components. You can find the React DevTools tab at the bottom of the *browser* section in CodeSandbox:
902
+
React DevTools let you check the props and the state of your React components. You can find the React DevTools tab at the bottom of the _browser_ section in CodeSandbox:
903
903
904
904

905
905
@@ -1094,7 +1094,7 @@ function Square({ value, onSquareClick }) {
1094
1094
}
1095
1095
```
1096
1096
1097
-
Now you'll connect the `onSquareClick` prop to a function in the `Board` component that you'll name `handleClick`. To connect `onSquareClick` to `handleClick` you'll pass a function to the `onSquareClick` prop of the first `Square` component:
1097
+
Now you'll connect the `onSquareClick` prop to a function in the `Board` component that you'll name `handleClick`. To connect `onSquareClick` to `handleClick` you'll pass a function to the `onSquareClick` prop of the first `Square` component:
1098
1098
1099
1099
```js {7}
1100
1100
exportdefaultfunctionBoard() {
@@ -1337,7 +1337,7 @@ The DOM `<button>` element's `onClick` attribute has a special meaning to React
1337
1337
1338
1338
Note how in `handleClick`, you call `.slice()` to create a copy of the `squares` array instead of modifying the existing array. To explain why, we need to discuss immutability and why immutability is important to learn.
1339
1339
1340
-
There are generally two approaches to changing data. The first approach is to *mutate* the data by directly changing the data's values. The second approach is to replace the data with a new copy which has the desired changes. Here is what it would look like if you mutated the `squares` array:
1340
+
There are generally two approaches to changing data. The first approach is to _mutate_ the data by directly changing the data's values. The second approach is to replace the data with a new copy which has the desired changes. Here is what it would look like if you mutated the `squares` array:
@@ -1590,7 +1590,7 @@ export default function Board() {
1590
1590
}
1591
1591
```
1592
1592
1593
-
Congratulations! You now have a working tic-tac-toe game. And you've just learned the basics of React too. So *you* are the real winner here. Here is what the code should look like:
1593
+
Congratulations! You now have a working tic-tac-toe game. And you've just learned the basics of React too. So _you_ are the real winner here. Here is what the code should look like:
1594
1594
1595
1595
<Sandpack>
1596
1596
@@ -1772,7 +1772,7 @@ export default function Game() {
1772
1772
}
1773
1773
```
1774
1774
1775
-
Note that you are removing the `exportdefault` keywords before the `functionBoard() {` declaration and adding them before the `functionGame() {` declaration. This tells your `src/index.js` file to use the `Game` component as the top-level component instead of your `Board` component. The additional `div`s returned by the `Game` component are making room for the game information you'll add to the board later.
1775
+
Note that you are removing the `exportdefault` keywords before the `functionBoard() {` declaration and adding them before the `functionGame() {` declaration. This tells your `index.js` file to use the `Game` component as the top-level component instead of your `Board` component. The additional `div`s returned by the `Game` component are making room for the game information you'll add to the board later.
1776
1776
1777
1777
Add some state to the `Game` component to track which player is next and the history of moves:
1778
1778
@@ -2073,7 +2073,7 @@ export default function Game() {
2073
2073
}
2074
2074
```
2075
2075
2076
-
You can see what your code should look like below. Note that you should see an error in the developer tools console that says:
2076
+
You can see what your code should look like below. Note that you should see an error in the developer tools console that says:
2077
2077
2078
2078
<ConsoleBlock level="warning">
2079
2079
Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `Game`.
@@ -2272,7 +2272,7 @@ to
2272
2272
<li>Alexa:5 tasks left</li>
2273
2273
```
2274
2274
2275
-
In addition to the updated counts, a human reading this would probably say that you swapped Alexa and Ben's ordering and inserted Claudia between Alexa and Ben. However, React is a computer program and does not know what you intended, so you need to specify a *key* property for each list item to differentiate each list item from its siblings. If your data was from a database, Alexa, Ben, and Claudia's database IDs could be used as keys.
2275
+
In addition to the updated counts, a human reading this would probably say that you swapped Alexa and Ben's ordering and inserted Claudia between Alexa and Ben. However, React is a computer program and does not know what you intended, so you need to specify a _key_ property for each list item to differentiate each list item from its siblings. If your data was from a database, Alexa, Ben, and Claudia's database IDs could be used as keys.
2276
2276
2277
2277
```js {1}
2278
2278
<li key={user.id}>
@@ -2915,4 +2915,4 @@ If you have extra time or want to practice your new React skills, here are some
2915
2915
1. When someone wins, highlight the three squares that caused the win (and when no one wins, display a message about the result being a draw).
2916
2916
1. Display the location for each move in the format (row, col) in the move history list.
2917
2917
2918
-
Throughout this tutorial, you've touched on React concepts including elements, components, props, and state. Now that you've seen how these concepts work when building a game, check out [Thinking in React](/learn/thinking-in-react) to see how the same React concepts work when building an app's UI.
2918
+
Throughout this tutorial, you've touched on React concepts including elements, components, props, and state. Now that you've seen how these concepts work when building a game, check out [Thinking in React](/learn/thinking-in-react) to see how the same React concepts work when building an app's UI.
0 commit comments