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
When Webpack comes across this syntax, it automatically starts code-splitting
104
104
your app. If you're using Create React App, this is already configured for you
105
-
and you can [start using it](https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#code-splitting) immediately. It's also supported
105
+
and you can [start using it](https://facebook.github.io/create-react-app/docs/code-splitting) immediately. It's also supported
106
106
out of the box in [Next.js](https://github.com/zeit/next.js/#dynamic-import).
107
107
108
108
If you're setting up Webpack yourself, you'll probably want to read Webpack's
Copy file name to clipboardExpand all lines: content/tutorial/tutorial.md
+22-42
Original file line number
Diff line number
Diff line change
@@ -31,8 +31,6 @@ The tutorial is divided into several sections:
31
31
32
32
You don't have to complete all of the sections at once to get the value out of this tutorial. Try to get as far as you can -- even if it's one or two sections.
33
33
34
-
It's fine to copy and paste code as you're following along the tutorial, but we recommend to type it by hand. This will help you develop a muscle memory and a stronger understanding.
35
-
36
34
### What Are We Building? {#what-are-we-building}
37
35
38
36
In this tutorial, we'll show how to build an interactive tic-tac-toe game with React.
@@ -188,7 +186,9 @@ The Square component renders a single `<button>` and the Board renders 9 squares
188
186
189
187
### Passing Data Through Props {#passing-data-through-props}
190
188
191
-
Just to get our feet wet, let's try passing some data from our Board component to our Square component.
189
+
To get our feet wet, let's try passing some data from our Board component to our Square component.
190
+
191
+
We strongly recommend typing code by hand as you're working through the tutorial and not using copy/paste. This will help you develop muscle memory and a stronger understanding.
192
192
193
193
In Board's `renderSquare` method, change the code to pass a prop called `value` to the Square:
194
194
@@ -242,7 +242,7 @@ class Square extends React.Component {
242
242
}
243
243
```
244
244
245
-
If we click on a Square now, we should get an alert in our browser.
245
+
If you click on a Square now, you should see an alert in your browser.
246
246
247
247
>Note
248
248
>
@@ -260,7 +260,7 @@ If we click on a Square now, we should get an alert in our browser.
260
260
>}
261
261
>```
262
262
>
263
-
>Notice how with `onClick={() => alert('click')}`, we're passing *a function* as the `onClick` prop. It only fires after a click. Forgetting `() =>` and writing `onClick={alert('click')}` is a common mistake, and would fire the alert every time the component re-renders.
263
+
>Notice how with `onClick={() => alert('click')}`, we're passing *a function* as the `onClick` prop. React will only call this function after a click. Forgetting `() =>` and writing `onClick={alert('click')}` is a common mistake, and would fire the alert every time the component re-renders.
264
264
265
265
As a next step, we want the Square component to "remember" that it got clicked, and fill it with an "X" mark. To "remember" things, components use **state**.
266
266
@@ -294,7 +294,7 @@ class Square extends React.Component {
294
294
Now we'll change the Square's `render` method to display the current state's value when clicked:
295
295
296
296
* Replace `this.props.value` with `this.state.value` inside the `<button>` tag.
297
-
* Replace the `() => alert()` event handler with `() => this.setState({value: 'X'})`.
297
+
* Replace the `onClick={...}` event handler with `onClick={() => this.setState({value: 'X'})}`.
298
298
* Put the `className` and `onClick` props on separate lines for better readability.
299
299
300
300
After these changes, the `<button>` tag that is returned by the Square's `render` method looks like this:
@@ -356,7 +356,9 @@ We may think that Board should just ask each Square for the Square's state. Alth
356
356
357
357
**To collect data from multiple children, or to have two child components communicate with each other, you need to declare the shared state in their parent component instead. The parent component can pass the state back down to the children by using props; this keeps the child components in sync with each other and with the parent component.**
358
358
359
-
Lifting state into a parent component is common when React components are refactored -- let's take this opportunity to try it out. We'll add a constructor to the Board and set the Board's initial state to contain an array with 9 nulls. These 9 nulls correspond to the 9 squares:
359
+
Lifting state into a parent component is common when React components are refactored -- let's take this opportunity to try it out.
360
+
361
+
Add a constructor to the Board and set the Board's initial state to contain an array of 9 nulls corresponding to the 9 squares:
360
362
361
363
```javascript{2-7}
362
364
class Board extends React.Component {
@@ -370,35 +372,9 @@ class Board extends React.Component {
370
372
renderSquare(i) {
371
373
return <Square value={i} />;
372
374
}
373
-
374
-
render() {
375
-
const status = 'Next player: X';
376
-
377
-
return (
378
-
<div>
379
-
<div className="status">{status}</div>
380
-
<div className="board-row">
381
-
{this.renderSquare(0)}
382
-
{this.renderSquare(1)}
383
-
{this.renderSquare(2)}
384
-
</div>
385
-
<div className="board-row">
386
-
{this.renderSquare(3)}
387
-
{this.renderSquare(4)}
388
-
{this.renderSquare(5)}
389
-
</div>
390
-
<div className="board-row">
391
-
{this.renderSquare(6)}
392
-
{this.renderSquare(7)}
393
-
{this.renderSquare(8)}
394
-
</div>
395
-
</div>
396
-
);
397
-
}
398
-
}
399
375
```
400
376
401
-
When we fill the board in later, the board will look something like this:
377
+
When we fill the board in later, the `this.state.squares` array will look something like this:
402
378
403
379
```javascript
404
380
[
@@ -432,7 +408,7 @@ Each Square will now receive a `value` prop that will either be `'X'`, `'O'`, or
432
408
433
409
Next, we need to change what happens when a Square is clicked. The Board component now maintains which squares are filled. We need to create a way for the Square to update the Board's state. Since state is considered to be private to a component that defines it, we cannot update the Board's state directly from Square.
434
410
435
-
To maintain the Board's state's privacy, we'll pass down a function from the Board to the Square. This function will get called when a Square is clicked. We'll change the `renderSquare` method in Board to:
411
+
Instead, we'll pass down a function from the Board to the Square, and we'll have Square call that function when a square is clicked. We'll change the `renderSquare` method in Board to:
436
412
437
413
```javascript{5}
438
414
renderSquare(i) {
@@ -478,11 +454,11 @@ When a Square is clicked, the `onClick` function provided by the Board is called
478
454
2. When the button is clicked, React will call the `onClick` event handler that is defined in Square's `render()` method.
479
455
3. This event handler calls `this.props.onClick()`. The Square's `onClick` prop was specified by the Board.
480
456
4. Since the Board passed `onClick={() => this.handleClick(i)}` to Square, the Square calls `this.handleClick(i)` when clicked.
481
-
5. We have not defined the `handleClick()` method yet, so our code crashes.
457
+
5. We have not defined the `handleClick()` method yet, so our code crashes. If you click a square now, you should see a red error screen saying something like "this.handleClick is not a function".
482
458
483
459
>Note
484
460
>
485
-
>The DOM `<button>` element's `onClick` attribute has a special meaning to React because it is a built-in component. For custom components like Square, the naming is up to you. We could name the Square's `onClick` prop or Board's `handleClick` method differently. In React, however, it is a convention to use `on[Event]` names for props which represent events and `handle[Event]` for the methods which handle the events.
461
+
>The DOM `<button>` element's `onClick` attribute has a special meaning to React because it is a built-in component. For custom components like Square, the naming is up to you. We could give any name to the Square's `onClick` prop or Board's `handleClick` method, and the code would work the same. In React, it's conventional to use `on[Event]` names for props which represent events and `handle[Event]` for the methods which handle the events.
486
462
487
463
When we try to click a Square, we should get an error because we haven't defined `handleClick` yet. We'll now add `handleClick` to the Board class:
488
464
@@ -539,7 +515,7 @@ class Board extends React.Component {
539
515
540
516
**[View the full code at this point](https://codepen.io/gaearon/pen/ybbQJX?editors=0010)**
541
517
542
-
After these changes, we're again able to click on the Squares to fill them. However, now the state is stored in the Board component instead of the individual Square components. When the Board's state changes, the Square components re-render automatically. Keeping the state of all squares in the Board component will allow it to determine the winner in the future.
518
+
After these changes, we're again able to click on the Squares to fill them, the same as we had before. However, now the state is stored in the Board component instead of the individual Square components. When the Board's state changes, the Square components re-render automatically. Keeping the state of all squares in the Board component will allow it to determine the winner in the future.
543
519
544
520
Since the Square components no longer maintain state, the Square components receive values from the Board component and inform the Board component when they're clicked. In React terms, the Square components are now **controlled components**. The Board has full control over them.
545
521
@@ -581,7 +557,7 @@ Detecting changes in mutable objects is difficult because they are modified dire
581
557
582
558
Detecting changes in immutable objects is considerably easier. If the immutable object that is being referenced is different than the previous one, then the object has changed.
583
559
584
-
#### Determining When to Re-render in React {#determining-when-to-re-render-in-react}
560
+
#### Determining When to Re-Render in React {#determining-when-to-re-render-in-react}
585
561
586
562
The main benefit of immutability is that it helps you build _pure components_ in React. Immutable data can easily determine if changes have been made which helps to determine when a component requires re-rendering.
587
563
@@ -611,7 +587,7 @@ We have changed `this.props` to `props` both times it appears.
611
587
612
588
>Note
613
589
>
614
-
>When we modified the Square to be a function component, we also changed `onClick={() => this.props.onClick()}` to a shorter `onClick={props.onClick}` (note the lack of parentheses on *both* sides). In a class, we used an arrow function to access the correct `this` value, but in a function component we don't need to worry about `this`.
590
+
>When we modified the Square to be a function component, we also changed `onClick={() => this.props.onClick()}` to a shorter `onClick={props.onClick}` (note the lack of parentheses on *both* sides).
615
591
616
592
### Taking Turns {#taking-turns}
617
593
@@ -643,7 +619,9 @@ Each time a player moves, `xIsNext` (a boolean) will be flipped to determine whi
643
619
}
644
620
```
645
621
646
-
With this change, "X"s and "O"s can take turns. Let's also change the "status" text in Board's `render` so that it displays which player has the next turn:
622
+
With this change, "X"s and "O"s can take turns. Try it!
623
+
624
+
Let's also change the "status" text in Board's `render` so that it displays which player has the next turn:
647
625
648
626
```javascript{2}
649
627
render() {
@@ -714,7 +692,7 @@ class Board extends React.Component {
714
692
715
693
### Declaring a Winner {#declaring-a-winner}
716
694
717
-
Now that we show which player's turn is next, we should also show when the game is won and there are no more turns to make. We can determine a winner by adding this helper function to the end of the file:
695
+
Now that we show which player's turn is next, we should also show when the game is won and there are no more turns to make. Copy this helper function and paste it at the end of the file:
718
696
719
697
```javascript
720
698
functioncalculateWinner(squares) {
@@ -738,6 +716,8 @@ function calculateWinner(squares) {
738
716
}
739
717
```
740
718
719
+
Given an array of 9 squares, this function will check for a winner and return `'X'`, `'O'`, or `null` as appropriate.
720
+
741
721
We will call `calculateWinner(squares)` in the Board's `render` function to check if a player has won. If a player has won, we can display text such as "Winner: X" or "Winner: O". We'll replace the `status` declaration in Board's `render` function with this code:
0 commit comments