/**
* 组件挂载后
* componentDidMount in hook way
*
* @export
* @param {() => any} onMount
* @returns
*/
import { useEffect } from "react";
export function useOnMount(onMount: () => any) {
return useEffect(() => {
if (onMount) {
//
onMount()
}
})
}
/**
* 组件卸载前
* componentWillUnmount in hook way
*
* @export
* @param {() => any} onUnmount
* @returns
*/
export function useOnUnmount(onUnmount: () => any) {
return useEffect(() => {
return () => onUnmount && onUnmount()
}, [])
}
/**
* 组件更新前
* componentDidUpdate in hook way
*
* @export
* @param {() => any} onUpdate
* @returns
*/
export function useOnUpdate(onUpdate: () => any) {
return useEffect(() => {
return () => onUpdate && onUpdate()
})
}
React Hooks实现组件生命周期,监听组件挂载、卸载、更新事件
最新推荐文章于 2024-07-20 05:52:04 发布