-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(useSetState): more detailed demo and api docs (#2087)
* docs(useSetState): more detail api docs * docs: more detail demo * docs: add genericity to api
- Loading branch information
Showing
4 changed files
with
77 additions
and
11 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,33 @@ | ||
/** | ||
* title: Updating with callback | ||
* desc: When using the callback to update, the previous state can be received, and the return value will be automatically merged. | ||
* | ||
* title.zh-CN: 使用回调更新 | ||
* desc.zh-CN: 通过回调进行更新,可以获取上一次的状态,并且也会自动合并返回的对象。 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { useSetState } from 'ahooks'; | ||
|
||
interface State { | ||
hello: string; | ||
count: number; | ||
} | ||
|
||
export default () => { | ||
const [state, setState] = useSetState<State>({ | ||
hello: 'world', | ||
count: 0, | ||
}); | ||
|
||
return ( | ||
<div> | ||
<pre>{JSON.stringify(state, null, 2)}</pre> | ||
<p> | ||
<button type="button" onClick={() => setState((prev) => ({ count: prev.count + 1 }))}> | ||
count + 1 | ||
</button> | ||
</p> | ||
</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