From e6146c656bbe7036924685ab59c8be49b422f204 Mon Sep 17 00:00:00 2001 From: Ika Date: Sat, 11 Dec 2021 22:57:22 +0800 Subject: [PATCH 01/23] docs(cn): scaling-up-with-reducer-and-context --- .../scaling-up-with-reducer-and-context.md | 425 ++++++++++-------- 1 file changed, 245 insertions(+), 180 deletions(-) diff --git a/beta/src/pages/learn/scaling-up-with-reducer-and-context.md b/beta/src/pages/learn/scaling-up-with-reducer-and-context.md index 2bc0877c3d..a74581ad56 100644 --- a/beta/src/pages/learn/scaling-up-with-reducer-and-context.md +++ b/beta/src/pages/learn/scaling-up-with-reducer-and-context.md @@ -1,29 +1,29 @@ --- -title: Scaling Up with Reducer and Context +title: 使用 Reducer 和 Context 来拓展你的应用 --- -Reducers let you consolidate a component's state update logic. Context lets you pass information deep down to other components. You can combine reducers and context together to manage state of a complex screen. +Reducer 可以整合组件的状态更新逻辑。Context 可以将信息深入传递给其他组件。你可以组合使用它们来共同管理一个复杂页面的状态。 -* How to combine a reducer with context -* How to avoid passing state and dispatch through props -* How to keep context and state logic in a separate file +* 如何结合使用 reducer 和 context +* 如何去避免通过 props 传递 state 和 dispatch +* 如何将 context 和状态逻辑保存在一个单独的文件中 -## Combining a reducer with context {/*combining-a-reducer-with-context*/} +## 结合使用 reducer 和 context {/*combining-a-reducer-with-context*/} -In this example from [the introduction to reducers](/learn/extracting-state-logic-into-a-reducer), the state is managed by a reducer. The reducer function contains all of the state update logic and is declared at the bottom of this file: +在 [reducer 介绍](/learn/extracting-state-logic-into-a-reducer)的例子里面,状态被reducer所管理。reducer函数包含了所有的状态更新逻辑并在此文件的底部声明: ```js App.js -import { useReducer } from 'react'; +import {useReducer} from 'react'; import AddTask from './AddTask.js'; import TaskList from './TaskList.js'; @@ -99,16 +99,16 @@ function tasksReducer(tasks, action) { let nextId = 3; const initialTasks = [ - { id: 0, text: 'Philosopher’s Path', done: true }, - { id: 1, text: 'Visit the temple', done: false }, - { id: 2, text: 'Drink matcha', done: false } + {id: 0, text: 'Philosopher’s Path', done: true}, + {id: 1, text: 'Visit the temple', done: false}, + {id: 2, text: 'Drink matcha', done: false} ]; ``` ```js AddTask.js -import { useState } from 'react'; +import {useState} from 'react'; -export default function AddTask({ onAddTask }) { +export default function AddTask({onAddTask}) { const [text, setText] = useState(''); return ( <> @@ -120,20 +120,21 @@ export default function AddTask({ onAddTask }) { + }}>Add + ) } ``` ```js TaskList.js -import { useState } from 'react'; +import {useState} from 'react'; export default function TaskList({ - tasks, - onChangeTask, - onDeleteTask -}) { + tasks, + onChangeTask, + onDeleteTask + }) { return (