-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from GuoXiCheng/dev-c
update context md
- Loading branch information
Showing
7 changed files
with
61 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
src/frontend/react/core-concepts/state-management/context.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Context | ||
|
||
::: details 在使用 Context 之前可以考虑的替代方案 | ||
|
||
::: code-group | ||
|
||
<<< @/../projects/react-sandbox/src/pages/state-demo/ContextReplaceWithProps.tsx [方案一:从传递 Props 开始] | ||
|
||
<<< @/../projects/react-sandbox/src/pages/state-demo/ContextReplaceWithChildren.tsx [方案二:抽象组件并 将 JSX 作为 children 传递 给它们] | ||
|
||
::: | ||
|
||
## 什么是 Context | ||
|
||
Context 是 React 中提供的可以在组件树中直接传递数据而无需逐层传递 Props 的功能。 | ||
|
||
## 基本用法 | ||
|
||
- 首先通过`createContext`方法**创建**一个 Context 对象 | ||
- 然后通过`Context.Provider`组件**提供**一个共享的值 | ||
- 最后通过`Context.Consumer`组件**消费**这个共享的值 | ||
|
||
:::code-group | ||
<<< @/../projects/react-sandbox/src/pages/state-demo/ContextTheme.ts[创建] | ||
|
||
<<< @/../projects/react-sandbox/src/pages/state-demo/ContextBasicProvider.tsx[提供] | ||
|
||
<<< @/../projects/react-sandbox/src/pages/state-demo/ContextBasicConsumer.tsx[消费] | ||
::: |
26 changes: 26 additions & 0 deletions
26
src/frontend/react/core-concepts/state-management/reducer-and-context.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Reducer 和 Context | ||
|
||
## 使用 Reducer 和 Context 拓展你的应用 | ||
|
||
Reducer 可以用来整合组件的状态更新逻辑。 | ||
|
||
Context 可以用来将数据深入传递给其他组件。 | ||
|
||
结合使用 Reducer 和 Context,可以共同管理一个复杂页面的状态。 | ||
|
||
## 基本用法 | ||
|
||
- 首先创建一个 Context 对象用于共享值。 | ||
- 然后创建一个 Reducer 函数用于处理状态更新逻辑。 | ||
- 接着创建一个 Provider 组件,将 Context 对象和 Reducer 函数传递给 Provider 组件。 | ||
- 最后在需要使用共享值的组件中使用 `useContext()` 消费共享值。 | ||
|
||
::: code-group | ||
<<< @/../projects/react-sandbox/src/pages/state-demo/CounterContext.ts [创建 Context 对象] | ||
|
||
<<< @/../projects/react-sandbox/src/pages/state-demo/CounterReducer.ts [创建 Reducer 函数] | ||
|
||
<<< @/../projects/react-sandbox/src/pages/state-demo/CounterProvider.tsx [创建 Provider 组件] | ||
|
||
<<< @/../projects/react-sandbox/src/pages/state-demo/CounterWithReducerContext.tsx [消费共享值] | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters