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
Copy file name to clipboardexpand all lines: beta/src/content/learn/adding-interactivity.md
+51-51
Original file line number
Diff line number
Diff line change
@@ -1,30 +1,30 @@
1
1
---
2
-
title: Adding Interactivity
2
+
title: 添加交互
3
3
---
4
4
5
5
<Intro>
6
6
7
-
Some things on the screen update in response to user input. For example, clicking an image gallery switches the active image. In React, data that changes over time is called *state.* You can add state to any component, and update it as needed. In this chapter, you'll learn how to write components that handle interactions, update their state, and display different output over time.
## Responding to events {/*responding-to-events*/}
23
+
## 响应事件 {#responding-to-events}
24
24
25
-
React lets you add *event handlers* to your JSX. Event handlers are your own functions that will be triggered in response to user interactions like clicking, hovering, focusing on form inputs, and so on.
Built-in components like `<button>`only support built-in browser events like `onClick`. However, you can also create your own components, and give their event handler props any application-specific names that you like.
## State: a component's memory {/*state-a-components-memory*/}
75
+
## State: 组件的记忆 {#state-a-components-memory}
76
76
77
-
Components often need to change what's on the screen as a result of an interaction. Typing into the form should update the input field, clicking "next" on an image carousel should change which image is displayed, clicking "buy" puts a product in the shopping cart. Components need to "remember" things: the current input value, the current image, the shopping cart. In React, this kind of component-specific memory is called *state.*
You can add state to a component with a [`useState`](/apis/react/useState) Hook. *Hooks* are special functions that let your components use React features (state is one of those features). The `useState` Hook lets you declare a state variable. It takes the initial state and returns a pair of values: the current state, and a state setter function that lets you update it.
Before your components are displayed on the screen, they must be rendered by React. Understanding the steps in this process will help you think about how your code executes and explain its behavior.
Imagine that your components are cooks in the kitchen, assembling tasty dishes from ingredients. In this scenario, React is the waiter who puts in requests from customers and brings them their orders. This process of requesting and serving UI has three steps:
1.**Triggering** a render (delivering the diner's order to the kitchen)
238
-
2.**Rendering** the component (preparing the order in the kitchen)
239
-
3.**Committing** to the DOM (placing the order on the table)
237
+
1.**触发**渲染(将食客的订单送到厨房)
238
+
2.**渲染**组件(接到厨房的订单)
239
+
3.**提交**到 DOM(将订单送到桌前)
240
240
241
241
<IllustrationBlocksequential>
242
-
<Illustrationcaption="Trigger"alt="React as a server in a restaurant, fetching orders from the users and delivering them to the Component Kitchen."src="/images/docs/illustrations/i_render-and-commit1.png" />
243
-
<Illustrationcaption="Render"alt="The Card Chef gives React a fresh Card component."src="/images/docs/illustrations/i_render-and-commit2.png" />
244
-
<Illustrationcaption="Commit"alt="React delivers the Card to the user at their table."src="/images/docs/illustrations/i_render-and-commit3.png" />
Unlike regular JavaScript variables, React state behaves more like a snapshot. Setting it does not change the state variable you already have, but instead triggers a re-render. This can be surprising at first!
React works this way to help you avoid subtle bugs. Here is a little chat app. Try to guess what happens if you press "Send" first and *then* change the recipient to Bob. Whose name will appear in the `alert`five seconds later?
[State as a Snapshot](/learn/state-as-a-snapshot)explains why this is happening. Setting state requests a new re-render, but does not change it in the already running code. So `score` continues to be `0` right after you call `setScore(score + 1)`.
You can fix this by passing an *updater function* when setting state. Notice how replacing `setScore(score + 1)`with`setScore(s => s + 1)`fixes the "+3" button. This is handy if you need to queue multiple state updates.
364
+
你可以通过在设置状态时传递一个 *更新器函数* 来解决这个问题。注意用 `setScore(s => s + 1)`替换`setScore(score + 1)`是如何修复“+3”按钮的。如果你需要排队进行多次状态更新,那么这非常方便。
## Updating objects in state {/*updating-objects-in-state*/}
404
+
## 更新状态中的对象 {#updating-objects-in-state}
405
405
406
-
State can hold any kind of JavaScript value, including objects. But you shouldn't change objects and arrays that you hold in the React state directly. Instead, when you want to update an object and array, you need to create a new one (or make a copy of an existing one), and then update the state to use that copy.
Usually, you will use the `...`spread syntax to copy objects and arrays that you want to change. For example, updating a nested object could look like this:
If copying objects in code gets tedious, you can use a library like [Immer](https://github.com/immerjs/use-immer)to reduce repetitive code:
## Updating arrays in state {/*updating-arrays-in-state*/}
635
+
## 更新状态中的数组 {#updating-arrays-in-state}
636
636
637
-
Arrays are another type of mutable JavaScript objects you can store in state and should treat as read-only. Just like with objects, when you want to update an array stored in state, you need to create a new one (or make a copy of an existing one), and then set state to use the new array:
@@ -701,7 +701,7 @@ function ItemList({ artworks, onToggle }) {
701
701
702
702
</Sandpack>
703
703
704
-
If copying arrays in code gets tedious, you can use a library like [Immer](https://github.com/immerjs/use-immer)to reduce repetitive code:
0 commit comments