-
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 #25 from GuoXiCheng/dev-c
update docs
- Loading branch information
Showing
5 changed files
with
68 additions
and
2 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
49 changes: 49 additions & 0 deletions
49
src/frontend/react/component-interaction/lifting-state-up.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,49 @@ | ||
# 状态提升 | ||
|
||
## 什么是状态提升 | ||
状态提升是React中用于状态管理的技术,当多个子组件需要共享状态时,可以将状态提升到它们最近共同父组件中,然后通过props将状态和状态更新函数传递给子组件。 | ||
|
||
### 状态提升用例 | ||
例如,通过将RMB和USD的值及其更新逻辑提升到共同的父组件中,再通过props传递状态和状态更新函数,实现了两个相互关联的输入框间的同步更新。 | ||
```jsx | ||
import { useState } from 'react'; | ||
|
||
const EXCHANGE_RATE = 7.2295; | ||
|
||
function Currency({ type, value, onChange }) { | ||
return ( | ||
<div> | ||
<label>{type}: </label> | ||
<input | ||
type="number" | ||
value={value} | ||
onChange={(e) => onChange(e.target.value)} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
function App() { | ||
const [rmbValue, setRmbValue] = useState(0); | ||
const [usdValue, setUsdValue] = useState(0); | ||
|
||
function handleRmbChange(rmb) { | ||
setRmbValue(rmb); | ||
setUsdValue((rmb / EXCHANGE_RATE).toFixed(4)); | ||
} | ||
|
||
function handleUsdChange(usd) { | ||
setUsdValue(usd); | ||
setRmbValue((usd * EXCHANGE_RATE).toFixed(4)); | ||
} | ||
|
||
return ( | ||
<> | ||
<Currency type="RMB" value={rmbValue} onChange={handleRmbChange} /> | ||
<Currency type="USD" value={usdValue} onChange={handleUsdChange} /> | ||
</> | ||
); | ||
} | ||
|
||
export default App; | ||
``` |
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 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 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
- React 状态提升 | ||
- React Styled Components 的基本用法 | ||
- React Styled Components 的条件渲染 |