From 636b141e7abd943d0d794bed38d173299cd80a52 Mon Sep 17 00:00:00 2001 From: qinhua <352484005@qq.com> Date: Mon, 8 Nov 2021 19:28:17 +0800 Subject: [PATCH 1/7] docs(cn): managing-state --- beta/src/pages/learn/managing-state.md | 254 ++++++++++++------------- 1 file changed, 126 insertions(+), 128 deletions(-) diff --git a/beta/src/pages/learn/managing-state.md b/beta/src/pages/learn/managing-state.md index 722468e4c7..1a069dc460 100644 --- a/beta/src/pages/learn/managing-state.md +++ b/beta/src/pages/learn/managing-state.md @@ -1,50 +1,53 @@ --- -title: Managing State +title: 状态管理 +translators: + - qinhua --- -As your application grows, it helps to be more intentional about how your state is organized and how the data flows between your components. Redundant or duplicate state is a common source of bugs. In this chapter, you'll learn how to structure your state well, how to keep your state update logic maintainable, and how to share state between distant components. +随着你的应用不断变大,去刻意的关注应用状态如何组织,以及数据如何在组件之间流动会对你很有帮助。冗余或重复的状态往往是缺陷的根源。 +在本节中,你将学习如何组织好状态,如何保持状态更新逻辑的可维护性,以及如何跨组件共享状态。 -* [How to think about UI changes as state changes](/learn/reacting-to-input-with-state) -* [How to structure state well](/learn/choosing-the-state-structure) -* [How "lift state up" to share it between components](/learn/sharing-state-between-components) -* [How to control whether the state gets preserved or reset](/learn/preserving-and-resetting-state) -* [How to consolidate complex state logic in a function](/learn/extracting-state-logic-into-a-reducer) -* [How to pass information without "prop drilling"](/learn/passing-data-deeply-with-context) -* [How to scale state management as your app grows](/learn/scaling-up-with-reducer-and-context) +* [如何将 UI 变更视为状态变更](/learn/reacting-to-input-with-state) +* [如何组织好状态](/learn/choosing-the-state-structure) +* [“状态提升”如何在组件之间共享状态](/learn/sharing-state-between-components) +* [如何控制状态的保留或重置](/learn/preserving-and-resetting-state) +* [如何在函数中整合复杂的状态逻辑](/learn/extracting-state-logic-into-a-reducer) +* [如何避免使用“逐层props”传递数据](/learn/passing-data-deeply-with-context) +* [如何随着应用的增长去扩展状态管理](/learn/scaling-up-with-reducer-and-context) -## Reacting to input with state +## 使用状态响应输入 {#reacting-to-input-with-state} -With React, you won't modify the UI from code directly. For example, you won't write commands like "disable the button", "enable the button", "show the success message", etc. Instead, you will describe the UI you want to see for the different visual states of your component ("initial state", "typing state", "success state"), and then trigger the state changes in response to user input. This is similar to how designers think about UI. +使用 React,你不用直接从代码层面修改 UI。例如,不用编写诸如“禁用按钮”、“启用按钮”、“显示成功消息”等命令。相反,你只需要描述组件在不同状态(“初始状态”、“输入状态”、“成功状态”)下希望展现的 UI,然后根据用户输入触发状态更改。这和设计师对 UI 的理解很相似。 -Here is a quiz form built using React. Note how it uses the `status` state variable to determine whether to enable or disable the submit button, and whether to show the success message instead. +下面是一个使用 React 编写的反馈表单。请注意看它是如何使用 `status` 这个状态变量来决定启用或禁用提交按钮,以及是否显示成功消息的。 ```js import { useState } from 'react'; -export default function Form() { - const [answer, setAnswer] = useState(''); +export default function FeedbackForm() { + const [message, setMessage] = useState(''); const [error, setError] = useState(null); const [status, setStatus] = useState('typing'); if (status === 'success') { - return

That's right!

+ return

感谢您!

} async function handleSubmit(e) { e.preventDefault(); setStatus('submitting'); try { - await submitForm(answer); + await submitForm(); setStatus('success'); } catch (err) { setStatus('typing'); @@ -53,45 +56,39 @@ export default function Form() { } function handleTextareaChange(e) { - setAnswer(e.target.value); + setMessage(e.target.value); } return ( - <> -

City quiz

-

- In which city is there a billboard that turns air into drinkable water? -

-
-