Skip to content

Commit 69ce9d3

Browse files
committed
finished translations "Defining the counter context in a separate file"
1 parent a35c374 commit 69ce9d3

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

src/content/6/zh/part6d.md

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ const App = () => {
720720

721721
context 的值现在被设置为一个包含了计数器的值和 *dispatch* 函数的数组。
722722

723-
Other components now access the context using the [useContext](https://beta.reactjs.org/reference/react/useContext) hook:
723+
<!--Other components now access the context using the [useContext](https://beta.reactjs.org/reference/react/useContext) hook:-->
724724

725725
其他的组件现在可以通过使用 [useContext](https://beta.reactjs.org/reference/react/useContext) 钩子来访问 context。
726726

@@ -749,9 +749,13 @@ const Button = ({ type, label }) => {
749749

750750
当前应用的代码可以在 [GitHub](https://github.com/fullstack-hy2020/hook-counter/tree/part6-2)*part6-2* 的分支中找到。
751751

752-
### Defining the counter context in a separate file
752+
### <!--Defining the counter context in a separate file-->
753753

754-
Our application has an annoying feature, that the functionality of the counter state management is partly defined in the <i>App</i> component. Now let's move everything related to the counter to <i>CounterContext.js</i>:
754+
### 在单独的文件中定义计数器的 context
755+
756+
<!--Our application has an annoying feature, that the functionality of the counter state management is partly defined in the <i>App</i> component. Now let's move everything related to the counter to <i>CounterContext.js</i>:-->
757+
758+
我们的应用有个令人讨厌的特点:计数器一部分状态管理的功能,是在 *APP* 组件中定义的。现在,让我们将和计数器有关的内容,都移动到 *CounterContext.js*
755759

756760
```js
757761
import { createContext, useReducer } from 'react'
@@ -784,9 +788,13 @@ export const CounterContextProvider = (props) => {
784788
export default CounterContext
785789
```
786790

787-
The file now exports, in addition to the <i>CounterContext</i> object corresponding to the context, the <i>CounterContextProvider</i> component, which is practically a context provider whose value is a counter and a dispatcher used for its state management.
791+
<!--The file now exports, in addition to the <i>CounterContext</i> object corresponding to the context, the <i>CounterContextProvider</i> component, which is practically a context provider whose value is a counter and a dispatcher used for its state management.-->
792+
793+
这个文件除了导出和 context 对应的 *CounterContext* 对象外,还导出了<i>CounterContextProvider</i> 组件,这个组件实际上是一个 context 提供者,它的值包括一个计数器和一个用于其状态管理的调度器。
794+
795+
<!--Let's enable the context provider by making a change in <i>index.js</i>:-->
788796

789-
Let's enable the context provider by making a change in <i>index.js</i>:
797+
让我们更新 *index.js* ,以启用 context 提供者:
790798

791799
```js
792800
import ReactDOM from 'react-dom/client'
@@ -800,9 +808,13 @@ ReactDOM.createRoot(document.getElementById('root')).render(
800808
)
801809
```
802810

803-
Now the context defining the value and functionality of the counter is available to <i>all</i> components of the application.
811+
<!--Now the context defining the value and functionality of the counter is available to <i>all</i> components of the application.-->
804812

805-
The <i>App</i> component is simplified to the following form:
813+
现在,定义了计数器的值和功能的 context,可以被应用中的*所有*组件使用。
814+
815+
<!--The <i>App</i> component is simplified to the following form:-->
816+
817+
*App* 组件被简化成如下的形式:
806818

807819
```js
808820
import Display from './components/Display'
@@ -824,7 +836,9 @@ const App = () => {
824836
export default App
825837
```
826838

827-
The context is still used in the same way, e.g. the component <i>Button</i> is defined as follows:
839+
<!--The context is still used in the same way, e.g. the component <i>Button</i> is defined as follows:-->
840+
841+
context 仍然按和此前相同的方法使用,例如, *Button* 组件可以通过如下的方式定义:
828842

829843
```js
830844
import { useContext } from 'react'
@@ -842,13 +856,17 @@ const Button = ({ type, label }) => {
842856
export default Button
843857
```
844858

845-
The <i>Button</i> component only needs the <i>dispatch</i> function of the counter, but it also gets the value of the counter from the context using the function <i>useContext</i>:
859+
<!--The <i>Button</i> component only needs the <i>dispatch</i> function of the counter, but it also gets the value of the counter from the context using the function <i>useContext</i>:-->
860+
861+
*Button* 组件仅需要计数器的 *dispatch* 函数,但是它也可以通过 *useContext* 从 context 中获取计数器的值:
846862

847863
```js
848864
const [counter, dispatch] = useContext(CounterContext)
849865
```
850866

851-
This is not a big problem, but it is possible to make the code a bit more pleasant and expressive by defining a couple of helper functions in the <i>CounterContext</i> file:
867+
<!--This is not a big problem, but it is possible to make the code a bit more pleasant and expressive by defining a couple of helper functions in the <i>CounterContext</i> file:-->
868+
869+
这不是个大问题,但是我们可以通过在 *CounterContext* 文件中编写一些辅助函数,使我们的代码更加优雅、清晰:
852870

853871
```js
854872
import { createContext, useReducer, useContext } from 'react' // highlight-line
@@ -870,7 +888,9 @@ export const useCounterDispatch = () => {
870888
// ...
871889
```
872890

873-
With the help of these helper functions, it is possible for the components that use the context to get hold of the part of the context that they need. The <i>Display</i> component changes as follows:
891+
<!--With the help of these helper functions, it is possible for the components that use the context to get hold of the part of the context that they need. The <i>Display</i> component changes as follows:-->
892+
893+
有了辅助函数的帮助,需要使用 context 的组件就可以只获得它们所需的部分。*Display* 组件的更新如下:
874894

875895
```js
876896
import { useCounterValue } from '../CounterContext' // highlight-line
@@ -886,7 +906,9 @@ const Display = () => {
886906
export default Display
887907
```
888908

889-
Component <i>Button</i> becomes:
909+
<!--Component <i>Button</i> becomes:-->
910+
911+
Button 组件更新为:
890912

891913
```js
892914
import { useCounterDispatch } from '../CounterContext' // highlight-line
@@ -903,11 +925,17 @@ const Button = ({ type, label }) => {
903925
export default Button
904926
```
905927

906-
The solution is quite elegant. The entire state of the application, i.e. the value of the counter and the code for managing it, is now isolated in the file <i>CounterContext</i>, which provides components with well-named and easy-to-use auxiliary functions for managing the state.
928+
<!--The solution is quite elegant. The entire state of the application, i.e. the value of the counter and the code for managing it, is now isolated in the file <i>CounterContext</i>, which provides components with well-named and easy-to-use auxiliary functions for managing the state.-->
929+
930+
这个解决方案非常优雅。整个应用的状态,即,计数器的值和管理值的代码,已经独立放置于 <i>CounterContext</i> 文件中,这个文件提供了命名良好和易于使用的辅助函数来管理状态。
931+
932+
<!--The final code for the application is in [GitHub](https://github.com/fullstack-hy2020/hook-counter/tree/part6-3) in the branch <i>part6-3</i>.-->
933+
934+
当前应用的代码可以在 [GitHub](https://github.com/fullstack-hy2020/hook-counter/tree/part6-3)*part6-3* 的分支中找到。
907935

908-
The final code for the application is in [GitHub](https://github.com/fullstack-hy2020/hook-counter/tree/part6-3) in the branch <i>part6-3</i>.
936+
<!--As a technical detail, it should be noted that the helper functions <i>useCounterValue</i> and <i>useCounterDispatch</i> are defined as [custom hooks](https://reactjs.org/docs/hooks-custom.html), because calling the hook function <i>useContext</i> is [possible](https://reactjs.org/docs/hooks -rules.html) only from React components or custom hooks. Custom Hooks, on the other hand, are JavaScript functions whose name must start with the string _use_. We will return to custom hooks in a little more detail in [part 7](http://localhost:8000/en/part7/custom_hooks) of the course.-->
909937

910-
As a technical detail, it should be noted that the helper functions <i>useCounterValue</i> and <i>useCounterDispatch</i> are defined as [custom hooks](https://reactjs.org/docs/hooks-custom.html), because calling the hook function <i>useContext</i> is [possible](https://reactjs.org/docs/hooks -rules.html) only from React components or custom hooks. Custom Hooks, on the other hand, are JavaScript functions whose name must start with the string _use_. We will return to custom hooks in a little more detail in [part 7](http://localhost:8000/en/part7/custom_hooks) of the course.
938+
作为一个技术细节,应当注意到辅助函数——<i>useCounterValue</i> <i>useCounterDispatch</i>,是 [自定义钩子(custom hooks](https://reactjs.org/docs/hooks-custom.html),因为[只能](https://reactjs.org/docs/hooks -rules.html)通过 React 组件或自定义钩子调用钩子函数 *useContext*。此外,自定义钩子是必须以 *use* 作为名称开头的 JavaScript 函数。我们将在这门课程的 [part 7](http://localhost:8000/en/part7/custom_hooks) 更深入地探讨自定义钩子。
911939

912940
</div>
913941

0 commit comments

Comments
 (0)