-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
dpyzo0o
committed
Dec 26, 2019
1 parent
8b6b5d2
commit cd59418
Showing
5 changed files
with
78 additions
and
1 deletion.
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,25 @@ | ||
import { useState, useCallback } from '@tarojs/taro'; | ||
|
||
/** | ||
* 切换 boolean 状态值 | ||
* | ||
* @param initialValue | ||
*/ | ||
function useToggle(initialValue: boolean): [boolean, (nextValue?: any) => void] { | ||
const [value, setValue] = useState<boolean>(initialValue); | ||
|
||
const toggle = useCallback( | ||
(nextValue?: any) => { | ||
if (typeof nextValue === 'boolean') { | ||
setValue(nextValue); | ||
} else { | ||
setValue(currentValue => !currentValue); | ||
} | ||
}, | ||
[setValue] | ||
); | ||
|
||
return [value, toggle]; | ||
} | ||
|
||
export default useToggle; |
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,18 @@ | ||
import { useEffect, useCallback } from '@tarojs/taro'; | ||
|
||
/** | ||
* 页面/组件卸载时的回调 | ||
* | ||
* @param callback | ||
*/ | ||
function useUnload(callback: () => void) { | ||
const memoizedCb = useCallback(callback, []); | ||
|
||
useEffect(() => { | ||
return () => { | ||
memoizedCb(); | ||
}; | ||
}, [memoizedCb]); | ||
} | ||
|
||
export default useUnload; |
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 @@ | ||
import Taro from '@tarojs/taro'; | ||
import { View, Button } from '@tarojs/components'; | ||
import { useSelector, useDispatch } from '@tarojs/redux'; | ||
import { RootState } from '~/redux/rootReducer'; | ||
import { increment } from '~/redux/slices/count'; | ||
import useUnload from '~/hooks/useUnload'; | ||
|
||
const Demo: Taro.FC = () => { | ||
const dispatch = useDispatch(); | ||
const count = useSelector((state: RootState) => state.count); | ||
|
||
useUnload(() => { | ||
console.log('demo unload'); | ||
}); | ||
|
||
return ( | ||
<View> | ||
<View>{count}</View> | ||
<Button type='primary' onClick={() => dispatch(increment(2))}> | ||
+ | ||
</Button> | ||
</View> | ||
); | ||
}; | ||
|
||
export default Demo; |
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