From 942f80af9e068902cedec075023069da04c1c727 Mon Sep 17 00:00:00 2001 From: BBboy01 <1156678002@qq.com> Date: Sat, 11 Dec 2021 21:20:28 +0800 Subject: [PATCH 01/11] docs(cn): reacting-to-input-with-state --- .../learn/reacting-to-input-with-state.md | 273 +++++++++--------- 1 file changed, 138 insertions(+), 135 deletions(-) diff --git a/beta/src/pages/learn/reacting-to-input-with-state.md b/beta/src/pages/learn/reacting-to-input-with-state.md index ead3ea5639..f17b368e95 100644 --- a/beta/src/pages/learn/reacting-to-input-with-state.md +++ b/beta/src/pages/learn/reacting-to-input-with-state.md @@ -1,37 +1,39 @@ --- -title: Reacting to Input with State +title: 状态响应输入 +translators: + - BBboy01 --- -React uses a declarative way to manipulate the UI. Instead of manipulating individual pieces of the UI directly, you describe the different states that your component can be in, and switch between them in response to the user input. This is similar to how designers think about the UI. +React 使用声明式模式控制 UI,也就是说,当你声明不同的状态时,你的组件可以根据不同的输入来相应输入,而不是去控制 UI 的各个部分。这与设计师如何去思考 UI 很相似。 -* How declarative UI programming differs from imperative UI programming -* How to enumerate the different visual states your component can be in -* How to trigger the changes between the different visual states from code +* 了解声明式 UI 编程与命令式 UI 编程有何不同 +* 了解如何列举你的组件可以处于的视图状态的不同 +* 了解如何在代码中触发不同视图状态的变化 -## How declarative UI compares to imperative {/*how-declarative-ui-compares-to-imperative*/} +## 声明式 UI 与 命令式 UI 的比较 {/*how-declarative-ui-compares-to-imperative*/} -When you design UI interactions, you probably think about how the UI *changes* in response to user actions. Consider a form that lets the user submit an answer: +当你设计 UI 交互时,可能会去思考 UI 如何根据用户的操作而响应*变化*。想象一个允许用户提交一个答案的表单: -* When you type something into a form, the "Submit" button **becomes enabled**. -* When you press "Submit", both form and the button **become disabled**, and a spinner **appears**. -* If the network request succeeds, the form **gets hidden**, and the "Thank you" message **appears**. -* If the network request fails, an error message **appears**, and the form **becomes enabled** again. +* 当你输入向表单数据时,“提交”按钮会随之变成**可用状态** +* 当你点击“提交”后,表单和提交按钮都会随之变成**不可用状态**,并且会加载动画会随之**出现** +* 如果网络请求成功,表单会随之**隐藏**,同时“提交成功”的信息会随之**出现** +* 如果网络请求失败,错误信息会随之**出现**,同时表单又变为**可用状态** -In **imperative programming**, the above corresponds directly to how you implement interaction. You have to write the exact instructions to manipulate the UI depending on what just happened. Here's another way to think about this: imagine riding next to someone in a car and telling them turn by turn where to go. +在 **命令式编程** 中,以上的过程直接告诉你如何去实现交互。你必须去根据要发生的事情写一些明确的命令去操作 UI。有这样一个场景去理解这些,当你坐在车里的某个人旁边,然后一步一步地告诉他该去哪。 - + -They don't know where you want to go, they just follow your commands. (And if you get the directions wrong, you end up in the wrong place!) It's called *imperative* because you have to "command" each element, from the spinner to the button, telling the computer *how* to update the UI. +他并不知道你想去哪,只想跟着命令行动。(并且如果你发出了错误的命令,那么你就会到达错误的地方)正因为你必须从加载动画到按钮地“命令”每个元素,所以这种告诉计算机*如何*去更新 UI 的编程方式被称为*命令式编程* -In this example of imperative UI programming, the form is built *without* React. It uses the built-in browser [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model): +在这个命令式 UI 编程的例子中,表单没有使用 React 生成,而是使用原生的 [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model): @@ -81,7 +83,7 @@ function disable(el) { } function submitForm(answer) { - // Pretend it's hitting the network. + // 假设正在进行网络请求 return new Promise((resolve, reject) => { setTimeout(() => { if (answer.toLowerCase() == 'istanbul') { @@ -111,17 +113,17 @@ textarea.oninput = handleTextareaChange; ```html public/index.html
-

City quiz

+

城市测验

- What city is located on two continents? + 哪个城市位于两个大陆上?


- - + +
-

That's right!

+

回答正确!