Skip to content

Commit 3187037

Browse files
committed
finished translations "useReducer"
1 parent d84dbae commit 3187037

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

src/content/6/zh/part6d.md

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,9 @@ React Query 一个多功能的库,根据我们已看到的情况,它简化
442442
</div>
443443

444444
<div class="tasks">
445-
### Exercises 6.19.-6.21.
445+
446+
### Exercises 6.20.-6.22.
447+
446448

447449

448450

@@ -509,16 +511,23 @@ Implement adding new anecdotes to the server using React Query. The application
509511

510512
<div class="content">
511513

514+
512515
### useReducer
513516

514-
So even if the application uses React Query, some kind of solution is usually needed to manage the rest of the frontend state (for example, the state of forms). Quite often, the state created with <i>useState</i> is a sufficient solution. Using Redux is of course possible, but there are other alternatives.
517+
<!--So even if the application uses React Query, some kind of solution is usually needed to manage the rest of the frontend state (for example, the state of forms). Quite often, the state created with <i>useState</i> is a sufficient solution. Using Redux is of course possible, but there are other alternatives.-->
515518

516-
Let's look at a simple counter application. The application displays the counter value, and offers three buttons to update the counter status:
519+
即使应用使用了 React query,通常还需要某种解决方案以管理前端的其他状态(例如,表单状态)。通常,利用 *useState* 创建的状态足以应对这种状况。使用 Redux 当然也没问题,但是我们还有其他选择。
520+
521+
<!--Let's look at a simple counter application. The application displays the counter value, and offers three buttons to update the counter status:-->
522+
523+
让我们看一个简单的计数应用。这个应用显示计数器的值,并提供三个按钮以更新计数器的状态:
517524

518525
![](../../images/6/63new.png)
519526

520527
We shall now implement the counter state management using a Redux-like state management mechanism provided by React's built-in [useReducer](https://beta.reactjs.org/reference/react/useReducer) hook. Code looks like the following:
521528

529+
现在,我们利用 React 内置的 [useReducer](https://beta.reactjs.org/reference/react/useReducer) 钩子来进行状态管理,useReducer 钩子具有类似 Redux 的状态管理机制。代码如下:
530+
522531
```js
523532
import { useReducer } from 'react'
524533

@@ -553,13 +562,17 @@ const App = () => {
553562
export default App
554563
```
555564

556-
The hook [useReducer](https://beta.reactjs.org/reference/react/useReducer) provides a mechanism to create a state for an application. The parameter for creating a state is the reducer function that handles state changes, and the initial value of the state:
565+
<!--The hook [useReducer](https://beta.reactjs.org/reference/react/useReducer) provides a mechanism to create a state for an application. The parameter for creating a state is the reducer function that handles state changes, and the initial value of the state:-->
566+
567+
[useReducer](https://beta.reactjs.org/reference/react/useReducer) 钩子提供了为应用创建状态的机制。创建一个状态所需的参数有:处理状态变化的 reducer 函数,以及状态的初始值:
557568

558569
```js
559570
const [counter, counterDispatch] = useReducer(counterReducer, 0)
560571
```
561572

562-
The reducer function that handles state changes is similar to Redux's reducers, i.e. the function gets as parameters the current state and the action that changes the state. The function returns the new state updated based on the type and possible contents of the action:
573+
<!--The reducer function that handles state changes is similar to Redux's reducers, i.e. the function gets as parameters the current state and the action that changes the state. The function returns the new state updated based on the type and possible contents of the action:-->
574+
575+
处理状态变化的 reducer 函数和 Redux 中的 reducers 类似,即,即该函数获得当前状态和改变此状态的 action 作为参数。该函数根据 action 的类型和其中的内容而返回更新后的状态。
563576

564577
```js
565578
const counterReducer = (state, action) => {
@@ -576,9 +589,13 @@ const counterReducer = (state, action) => {
576589
}
577590
```
578591

579-
In our example, actions have nothing but a type. If the action's type is <i>INC</i>, it increases the value of the counter by one, etc. Like Redux's reducers, actions can also contain arbitrary data, which is usually put in the action's <i>payload</i> field.
592+
I<!--n our example, actions have nothing but a type. If the action's type is <i>INC</i>, it increases the value of the counter by one, etc. Like Redux's reducers, actions can also contain arbitrary data, which is usually put in the action's <i>payload</i> field.-->
593+
594+
在我们的例子中,action 只有类型这一个字段。如果动作的类型是 *INC*,它就会将计数器的值增加 1,其他也类似。正如 Redux 的 reducers,actions 也可以包含任意的数据,这些数据通常都被放在 *payload* 字段中。
580595

581-
The function <i>useReducer</i> returns an array that contains an element to access the current value of the state (first element of the array), and a <i>dispatch</i> function (second element of the array) to change the state:
596+
<!--The function <i>useReducer</i> returns an array that contains an element to access the current value of the state (first element of the array), and a <i>dispatch</i> function (second element of the array) to change the state:-->
597+
598+
<i>useReducer</i> 函数返回一个数组,该数组包含一个可以访问当前状态值的元素(数组的第一个元素),以及一个用于改变状态的 *dispatch* 函数(数组的第二个元素):
582599

583600
```js
584601
const App = () => {
@@ -597,13 +614,17 @@ const App = () => {
597614
}
598615
```
599616

600-
As can be seen the state change is done exactly as in Redux, the dispatch function is given the appropriate state-changing action as a parameter:
617+
<!--As can be seen the state change is done exactly as in Redux, the dispatch function is given the appropriate state-changing action as a parameter:-->
618+
619+
我们对状态的更改顺利完成,正如利用 Redux 一样。恰当的状态改变类型被传入 dispatch 函数作为参数:
601620

602621
```js
603622
counterDispatch({ type: "INC" })
604623
```
605624

606-
The current code for the application is in the repository [https://github.com/fullstack-hy2020/hook-counter](https://github.com/fullstack-hy2020/hook-counter/tree/part6-1) in the branch <i>part6-1</i>.
625+
<!--The current code for the application is in the repository [https://github.com/fullstack-hy2020/hook-counter](https://github.com/fullstack-hy2020/hook-counter/tree/part6-1) in the branch <i>part6-1</i>.-->
626+
627+
当前应用的代码可以在 [GitHub](https://github.com/fullstack-hy2020/hook-counter/tree/part6-1)*part6-1* 的分支中找到。
607628

608629
### Using context for passing the state to components
609630

0 commit comments

Comments
 (0)