diff --git a/content/docs/hooks-state.md b/content/docs/hooks-state.md index c910268ac1..8395a6d768 100644 --- a/content/docs/hooks-state.md +++ b/content/docs/hooks-state.md @@ -1,20 +1,20 @@ --- id: hooks-state -title: Using the State Hook +title: 使用 State Hook permalink: docs/hooks-state.html next: hooks-effect.html prev: hooks-overview.html --- -*Hooks* are an upcoming feature that lets you use state and other React features without writing a class. They're currently in React v16.8.0-alpha.1. +*Hook* 是一个即将到来的特性,它让你不用写 class 也可以使用 state 和其他 React 特性。现在可以在 React v16.8.0-alpha.1 版本中使用它。 -The [previous page](/docs/hooks-intro.html) introduced Hooks with this example: +[上一页](/docs/hooks-intro.html)用这个例子介绍了 Hook: ```js{4-5} import { useState } from 'react'; function Example() { - // Declare a new state variable, which we'll call "count" + // 声明一个叫 "count" 的 state 变量 const [count, setCount] = useState(0); return ( @@ -28,11 +28,11 @@ function Example() { } ``` -We'll start learning about Hooks by comparing this code to an equivalent class example. +我们将通过将这段代码与一个等价的 class 示例进行比较来开始学习有关 Hook 的知识。 -## Equivalent Class Example +## 等价的 class 示例 -If you used classes in React before, this code should look familiar: +如果你之前在 React 中使用过 class,这段代码看起来应该很熟悉: ```js class Example extends React.Component { @@ -56,39 +56,39 @@ class Example extends React.Component { } ``` -The state starts as `{ count: 0 }`, and we increment `state.count` when the user clicks a button by calling `this.setState()`. We'll use snippets from this class throughout the page. +state 初始值为 `{ count: 0 }` ,当用户点击按钮后,我们通过调用 `this.setState()` 来增加 `state.count`。后文中我们将使用这个 class 的代码片段做范例。 ->Note +>注意 > ->You might be wondering why we're using a counter here instead of a more realistic example. This is to help us focus on the API while we're still making our first steps with Hooks. +>你可能想知道为什么我们在这里使用一个计数器例子而不一个更实际的示例。因为我们还只是初步接触 Hook ,这可以帮助我们将注意力集中到 API 本身。 -## Hooks and Function Components +## Hook 和函数定义组件 -As a reminder, function components in React look like this: +复习一下, React 的函数定义组件是这样的: ```js const Example = (props) => { - // You can use Hooks here! + // 你可以在这使用 Hooks return
; } ``` -or this: +或是这样: ```js function Example(props) { - // You can use Hooks here! + // 你可以在这使用 Hooks return
; } ``` -You might have previously known these as "stateless components". We're now introducing the ability to use React state from these, so we prefer the name "function components". +你之前可能把它们叫做“无状态组件”。但现在我们为它们引入了使用 React state 的能力,所以我们更喜欢叫它"函数定义组件"。 -Hooks **don't** work inside classes. But you can use them instead of writing classes. +Hook 在 class 内部是**不**起作用的。但你可以使用它们来取代 class 。 -## What's a Hook? +## Hook 是什么? -Our new example starts by importing the `useState` Hook from React: +我们的新示例首先从 React 导入 `useState` Hook ```js{1} import { useState } from 'react'; @@ -98,17 +98,17 @@ function Example() { } ``` -**What is a Hook?** A Hook is a special function that lets you "hook into" React features. For example, `useState` is a Hook that lets you add React state to function components. We'll learn other Hooks later. +**Hook 是什么?** Hook 是一个特殊的函数,它可以让你 `钩入` React 的特性。例如,`useState` 是一个让你添加 React state 到函数定义组件的 Hook。稍后我们将学习其他 Hook。 -**When would I use a Hook?** If you write a function component and realize you need to add some state to it, previously you had to convert it to a class. Now you can use a Hook inside the existing function component. We're going to do that right now! +**什么时候我会用 Hook?** 如果你在写一个函数定义组件并意识到需要向其添加一些 state,如果是以前的话你必须把它转化为一个 class。现在你可以在现有的函数定义组件中使用 Hook。我们现在就去做! ->Note: +>注意: > ->There are some special rules about where you can and can't use Hooks within a component. We'll learn them in [Rules of Hooks](/docs/hooks-rules.html). +>在组件中有些特殊的规则,规定什么地方能使用 Hook,什么地方不能使用。我们将在 [Hook 规范](/docs/hooks-rules.html)中学习它们。 -## Declaring a State Variable +## 声明一个 State 变量 -In a class, we initialize the `count` state to `0` by setting `this.state` to `{ count: 0 }` in the constructor: +在 class 中,我们通过在构造函数中设置 `this.state` 为 `{ count: 0 }` 来初始化 `count` state 为 `0`: ```js{4-6} class Example extends React.Component { @@ -120,58 +120,58 @@ class Example extends React.Component { } ``` -In a function component, we have no `this`, so we can't assign or read `this.state`. Instead, we call the `useState` Hook directly inside our component: +在函数定义组件中,我们没有 `this`,所以我们不能分配或读取 `this.state`。我们直接在组件中调用 `useState` Hook: ```js{4,5} import { useState } from 'react'; function Example() { - // Declare a new state variable, which we'll call "count" + // 声明一个叫 “count” 的 state 变量 const [count, setCount] = useState(0); ``` -**What does calling `useState` do?** It declares a "state variable". Our variable is called `count` but we could call it anything else, like `banana`. This is a way to "preserve" some values between the function calls — `useState` is a new way to use the exact same capabilities that `this.state` provides in a class. Normally, variables "disappear" when the function exits but state variables are preserved by React. +**`useState` 方法做了什么?** 它定义一个 “state 变量”。我们的变量叫 `count`, 但我们可以把它叫做任意的东西,像 `banana`。这是一种在函数调用之间保存一些值的方式—— `useState` 是一种新方法,它和 class 里面的 `this.state` 提供的功能完全相同。一般来说,在函数退出后变量就就会"消失",但 state 变量会被 React 保留。 -**What do we pass to `useState` as an argument?** The only argument to the `useState()` Hook is the initial state. Unlike with classes, the state doesn't have to be an object. We can keep a number or a string if that's all we need. In our example, we just want a number for how many times the user clicked, so pass `0` as initial state for our variable. (If we wanted to store two different values in state, we would call `useState()` twice.) +**我们应该传递哪些参数给 `useState`?** `useState()` 方法里面唯一的参数就是初始 state。不用于 class,我们可以按照需要使用数字或字符串,而不一定是要一个对象。在我们示例中,只要一个数字来记录用户点击次数,所以我们传了 `0` 作为变量的初始 state。(如果我们想要在 state 中存储两个不同的变量,只需调用 `useState()`两次即可。) -**What does `useState` return?** It returns a pair of values: the current state and a function that updates it. This is why we write `const [count, setCount] = useState()`. This is similar to `this.state.count` and `this.setState` in a class, except you get them in a pair. If you're not familiar with the syntax we used, we'll come back to it [at the bottom of this page](/docs/hooks-state.html#tip-what-do-square-brackets-mean). +**`useState` 方法的返回值是什么?** 它返回一对值:当前 state 和一个更新 state 的函数。这就是我们写 `const [count, setCount] = useState()` 的原因。这跟 class 里面 `this.state.count` 和 `this.setState` 类似,唯一区别就是你可以成对的获取它们。如果你不熟悉我们使用的语法,我们会在[这页的底部](/docs/hooks-state.html#tip-what-do-square-brackets-mean)介绍它。 -Now that we know what the `useState` Hook does, our example should make more sense: +既然我们知道了 `useState` 的作用,我们的示例应该更容易理解了: ```js{4,5} import { useState } from 'react'; function Example() { - // Declare a new state variable, which we'll call "count" + // 声明一个叫 "count" 的 state 变量 const [count, setCount] = useState(0); ``` -We declare a state variable called `count`, and set it to `0`. React will remember its current value between re-renders, and provide the most recent one to our function. If we want to update the current `count`, we can call `setCount`. +我们声明了一个叫 `count` 的 state 变量,然后把它设为 `0`。React 会在重复渲染时记住它当前的值,并且提供最新的值给我们的函数。我们可以通过调用 `setCount` 来更新当前的 `count`。 ->Note +>注意 > ->You might be wondering: why is `useState` not named `createState` instead? +>你可能想知道:为什么叫 `useState` 而不叫 `createState`? > ->"Create" wouldn't be quite accurate because the state is only created the first time our component renders. During the next renders, `useState` gives us the current state. Otherwise it wouldn't be "state" at all! There's also a reason why Hook names *always* start with `use`. We'll learn why later in the [Rules of Hooks](/docs/hooks-rules.html). +>"Create" 可能不是很准确,因为 state 只在组件首次渲染的时候被创建。在下一次重新渲染时,`useState` 返回给我们当前的 state。否则它就不是 “state”了!这也是 Hook 的名字*总是*以 `use` 开头的一个原因。我们将在后面的 [Hook 规范](/docs/hooks-rules.html)中了解原因。 -## Reading State +## 读取 State -When we want to display the current count in a class, we read `this.state.count`: +当我们想在 class 中显示当前的 count,我们读取 `this.state.count`: ```js

You clicked {this.state.count} times

``` -In a function, we can use `count` directly: +在函数中,我们可以直接用 `count`: ```js

You clicked {count} times

``` -## Updating State +## 更新 State -In a class, we need to call `this.setState()` to update the `count` state: +在 class 中,我们需要调用 `this.setState()` 来更新 `count` 值: ```js{1} ``` -In a function, we already have `setCount` and `count` as variables so we don't need `this`: +在函数中,我们已经有了 `setCount` 和 `count` 变量,所以我们不需要 `this`: ```js{1} ``` -## Recap +## 总结 -Let's now **recap what we learned line by line** and check our understanding. +现在让我们来**仔细回顾一下学到的知识**,看下我们是否真正理解了。