-
-
Notifications
You must be signed in to change notification settings - Fork 70
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 #114 from heluxjs/doc
[released 4.0.3]
- Loading branch information
Showing
38 changed files
with
397 additions
and
63 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 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,28 @@ | ||
--- | ||
group: | ||
title: 帮助 | ||
order: 4 | ||
order: 0 | ||
--- | ||
|
||
# watchEffect | ||
|
||
`watchEffect`用于观察数据变化,并做对应的处理逻辑,观察的粒度可以任意定制。 | ||
|
||
区别于`watch`,`watchEffect`回调会立即执行,自动对首次运行时函数内读取到的值完成变化监听。 | ||
|
||
:::info | ||
本章节展示基础用法,更多用法查阅[指南/观察](/guide/watch)或[基础api/观察](/api/base/watch) | ||
::: | ||
|
||
## 基础用法 | ||
|
||
```ts | ||
import { watchEffect, getSnap } from ' helux '; | ||
const [priceState, setPrice] = share({ a: 1 }); | ||
|
||
// 观察 priceState.a 的变化 | ||
watchEffect(() => { | ||
console.log(`found price.a changed from ${getSnap(priceState).a} to ${priceState.a}`); | ||
}); | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
order: 4 | ||
--- | ||
|
||
# useWatchEffect | ||
|
||
`useWatchEffect` 功能同 `watchEffect``一样,区别在于 `useWatchEffect` 会立即执行回调,自动对首次运行时函数内读取到的值完成变化监听。 | ||
|
||
:::info | ||
其他使用方式可参考[watchEffect](/api/hooks/use-effect) | ||
::: | ||
|
||
|
||
## 基础用法 | ||
|
||
:::info | ||
`useWatchEffect`回调的首次运行的执行时机是在组件挂载完毕后才执行 | ||
::: | ||
|
||
```tsx | ||
/** | ||
* defaultShowCode: true | ||
*/ | ||
import { share, useMutable, useWatchEffect, getSnap } from 'helux'; | ||
|
||
const [priceState, setState, ctx] = share({ a: 1, b: { b1: { b2: 200 } } }); | ||
|
||
function changeA() { | ||
setState((draft) => { | ||
draft.a += 1; | ||
}); | ||
} | ||
|
||
export default function Comp(props: any) { | ||
const [ state, setState] = useMutable({tip:'1'}) | ||
useWatchEffect(() => { | ||
setState(draft=>{ | ||
draft.tip = `priceState.a changed from ${getSnap(priceState).a} to ${priceState.a}`; | ||
}); | ||
}); | ||
|
||
return ( | ||
<div> | ||
<button onClick={changeA}>changeA</button> | ||
<h1>tip: {state.tip}</h1> | ||
</div> | ||
); | ||
} | ||
``` |
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
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
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
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
Oops, something went wrong.