Skip to content

Commit

Permalink
Merge branch 'master' into handling-events
Browse files Browse the repository at this point in the history
  • Loading branch information
simsim0709 authored Mar 5, 2019
2 parents b439f7f + 3bfe77d commit 6fe9d72
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 57 deletions.
2 changes: 1 addition & 1 deletion content/docs/code-splitting.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ import("./math").then(math => {
When Webpack comes across this syntax, it automatically starts code-splitting
your app. If you're using Create React App, this is already configured for you
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
and you can [start using it](https://facebook.github.io/create-react-app/docs/code-splitting) immediately. It's also supported
out of the box in [Next.js](https://github.com/zeit/next.js/#dynamic-import).

If you're setting up Webpack yourself, you'll probably want to read Webpack's
Expand Down
40 changes: 34 additions & 6 deletions content/languages.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Status enums indicate what percentage of "core" content has been translated:
# 0: Incomplete (049%)
# 1: Partially complete (50–94%)
# 2: Complete (95–100%)
# 0: Incomplete (0-49%)
# 1: Partially complete (50-99%)
# 2: Complete (100%)

- name: English
translated_name: English
Expand All @@ -27,6 +27,10 @@
translated_name: Deutsch
code: de
status: 0
- name: Greek
translated_name: Ελληνικά
code: el
status: 0
- name: Spanish
translated_name: Español
code: es
Expand All @@ -38,6 +42,10 @@
- name: French
translated_name: Français
code: fr
status: 1
- name: Gujarati
translated_name: ગુજરાતી
code: gu
status: 0
- name: Hebrew
translated_name: עברית
Expand All @@ -62,11 +70,23 @@
- name: Japanese
translated_name: 日本語
code: ja
status: 1
status: 2
- name: Central Khmer
translated_name: ភាសាខ្មែរ
code: km
status: 0
- name: Korean
translated_name: 한국어
code: ko
status: 0
- name: Kurdish
translated_name: کوردی‎
code: ku
status: 0
- name: Lithuanian
translated_name: Lietuvių kalba
code: lt
status: 0
- name: Malayalam
translated_name: മലയാളം
code: ml
Expand Down Expand Up @@ -98,7 +118,7 @@
- name: Russian
translated_name: Русский
code: ru
status: 0
status: 1
- name: Sinhala
translated_name: සිංහල
code: si
Expand All @@ -107,13 +127,21 @@
translated_name: தமிழ்
code: ta
status: 0
- name: Telugu
translated_name: తెలుగు
code: te
status: 0
- name: Turkish
translated_name: Türkçe
code: tr
status: 0
- name: Ukrainian
translated_name: Українська
code: uk
status: 1
- name: Urdu
translated_name: اردو
code: ur
status: 0
- name: Uzbek
translated_name: Oʻzbekcha
Expand All @@ -126,7 +154,7 @@
- name: Simplified Chinese
translated_name: 简体中文
code: zh-hans
status: 0
status: 1
- name: Traditional Chinese
translated_name: 繁體中文
code: zh-hant
Expand Down
64 changes: 22 additions & 42 deletions content/tutorial/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ The tutorial is divided into several sections:

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.

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.

### What Are We Building? {#what-are-we-building}

In this tutorial, we'll show how to build an interactive tic-tac-toe game with React.
Expand Down Expand Up @@ -188,7 +186,9 @@ The Square component renders a single `<button>` and the Board renders 9 squares

### Passing Data Through Props {#passing-data-through-props}

Just to get our feet wet, let's try passing some data from our Board component to our Square component.
To get our feet wet, let's try passing some data from our Board component to our Square component.

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.

In Board's `renderSquare` method, change the code to pass a prop called `value` to the Square:

Expand Down Expand Up @@ -242,7 +242,7 @@ class Square extends React.Component {
}
```

If we click on a Square now, we should get an alert in our browser.
If you click on a Square now, you should see an alert in your browser.

>Note
>
Expand All @@ -260,7 +260,7 @@ If we click on a Square now, we should get an alert in our browser.
>}
>```
>
>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.
>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.
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**.
Expand Down Expand Up @@ -294,7 +294,7 @@ class Square extends React.Component {
Now we'll change the Square's `render` method to display the current state's value when clicked:

* Replace `this.props.value` with `this.state.value` inside the `<button>` tag.
* Replace the `() => alert()` event handler with `() => this.setState({value: 'X'})`.
* Replace the `onClick={...}` event handler with `onClick={() => this.setState({value: 'X'})}`.
* Put the `className` and `onClick` props on separate lines for better readability.

After these changes, the `<button>` tag that is returned by the Square's `render` method looks like this:
Expand Down Expand Up @@ -356,7 +356,9 @@ We may think that Board should just ask each Square for the Square's state. Alth

**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.**

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:
Lifting state into a parent component is common when React components are refactored -- let's take this opportunity to try it out.

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:

```javascript{2-7}
class Board extends React.Component {
Expand All @@ -370,35 +372,9 @@ class Board extends React.Component {
renderSquare(i) {
return <Square value={i} />;
}
render() {
const status = 'Next player: X';
return (
<div>
<div className="status">{status}</div>
<div className="board-row">
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
<div className="board-row">
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div className="board-row">
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
</div>
);
}
}
```

When we fill the board in later, the board will look something like this:
When we fill the board in later, the `this.state.squares` array will look something like this:

```javascript
[
Expand Down Expand Up @@ -432,7 +408,7 @@ Each Square will now receive a `value` prop that will either be `'X'`, `'O'`, or

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.

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

```javascript{5}
renderSquare(i) {
Expand Down Expand Up @@ -478,11 +454,11 @@ When a Square is clicked, the `onClick` function provided by the Board is called
2. When the button is clicked, React will call the `onClick` event handler that is defined in Square's `render()` method.
3. This event handler calls `this.props.onClick()`. The Square's `onClick` prop was specified by the Board.
4. Since the Board passed `onClick={() => this.handleClick(i)}` to Square, the Square calls `this.handleClick(i)` when clicked.
5. We have not defined the `handleClick()` method yet, so our code crashes.
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".

>Note
>
>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.
>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.
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:

Expand Down Expand Up @@ -539,7 +515,7 @@ class Board extends React.Component {

**[View the full code at this point](https://codepen.io/gaearon/pen/ybbQJX?editors=0010)**

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

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.

Expand Down Expand Up @@ -581,7 +557,7 @@ Detecting changes in mutable objects is difficult because they are modified dire

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.

#### Determining When to Re-render in React {#determining-when-to-re-render-in-react}
#### Determining When to Re-Render in React {#determining-when-to-re-render-in-react}

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.

Expand Down Expand Up @@ -611,7 +587,7 @@ We have changed `this.props` to `props` both times it appears.

>Note
>
>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`.
>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).
### Taking Turns {#taking-turns}

Expand Down Expand Up @@ -643,7 +619,9 @@ Each time a player moves, `xIsNext` (a boolean) will be flipped to determine whi
}
```

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:
With this change, "X"s and "O"s can take turns. Try it!

Let's also change the "status" text in Board's `render` so that it displays which player has the next turn:

```javascript{2}
render() {
Expand Down Expand Up @@ -714,7 +692,7 @@ class Board extends React.Component {

### Declaring a Winner {#declaring-a-winner}

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

```javascript
function calculateWinner(squares) {
Expand All @@ -738,6 +716,8 @@ function calculateWinner(squares) {
}
```

Given an array of 9 squares, this function will check for a winner and return `'X'`, `'O'`, or `null` as appropriate.

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:

```javascript{2-8}
Expand Down
4 changes: 2 additions & 2 deletions content/warnings/special-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ layout: single
permalink: warnings/special-props.html
---

Most props on a JSX element are passed on to the component, however, there are two special props (`ref` and `key`) which are used by React, and are thus not forwarded to the component.
JSX 엘리먼트 대부분의 props는 컴포넌트로 전달되지만 React에서 사용하는 두 개의 특수 props(`ref` `key`)는 컴포넌트로 전달되지 않습니다.

For instance, attempting to access `this.props.key` from a component (i.e., the render function or [propTypes](/docs/typechecking-with-proptypes.html#proptypes)) is not defined. If you need to access the same value within the child component, you should pass it as a different prop (ex: `<ListItemWrapper key={result.id} id={result.id} />`). While this may seem redundant, it's important to separate app logic from reconciling hints.
예를 들어, 컴포넌트에서 `this.props.key`render 함수나 [propTypes](/docs/typechecking-with-proptypes.html#proptypes)에서 접근하면 그 값은 `undefined` 입니다. 자식 컴포넌트 내에서 같은 값에 액세스하고 싶다면 다른 프로퍼티로 전달해야 합니다(예시: `<ListItemWrapper key={result.id} id={result.id} />`). 불필요해 보일지 모르지만, 재조정을 위해 사용되는 속성과 앱 로직을 분리하는 것은 중요합니다.
12 changes: 6 additions & 6 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Home extends Component {
return (
<Layout location={location}>
<TitleAndMetaTags
title="React &ndash; A JavaScript library for building user interfaces"
title="React &ndash; 사용자 인터페이스를 만들기 위한 JavaScript 라이브러리"
ogUrl={createOgUrl('index.html')}
/>
<div css={{width: '100%'}}>
Expand Down Expand Up @@ -133,7 +133,7 @@ class Home extends Component {
fontSize: 30,
},
}}>
A JavaScript library for building user interfaces
사용자 인터페이스를 만들기 위한 JavaScript 라이브러리
</p>
<Flex
valign="center"
Expand All @@ -148,12 +148,12 @@ class Home extends Component {
<ButtonLink
to="/docs/getting-started.html"
type="primary">
Get Started
시작하기
</ButtonLink>
</CtaItem>
<CtaItem>
<ButtonLink to="/tutorial/tutorial.html" type="secondary">
Take the Tutorial
자습서 읽어보기
</ButtonLink>
</CtaItem>
</Flex>
Expand Down Expand Up @@ -285,12 +285,12 @@ class Home extends Component {
<Flex valign="center">
<CtaItem>
<ButtonLink to="/docs/getting-started.html" type="primary">
Get Started
시작하기
</ButtonLink>
</CtaItem>
<CtaItem>
<ButtonLink to="/tutorial/tutorial.html" type="secondary">
Take the Tutorial
자습서 읽어보기
</ButtonLink>
</CtaItem>
</Flex>
Expand Down

0 comments on commit 6fe9d72

Please sign in to comment.