Skip to content
On this page

useEffectOnce

useEffect 的快捷方式,只在 onMount 和 onBeforeUnmount 阶段执行的 Hook。

Demo

Please open console
<template>
<div>Please open console</div>
</template>

<script lang="ts" setup>
import { useEffectOnce } from 'v3-use'
useEffectOnce(() => {
console.log('mounted')
return () => {
console.log('beforeUnmount')
}
})
</script>

Usage

import { useEffectOnce } from 'v3-use'
useEffectOnce(() => {
  console.log('mounted 执行')
  return () => {
    console.log('beforeUnmount 执行')
  }
})
import { useEffectOnce } from 'v3-use'
useEffectOnce(() => {
  console.log('mounted 执行')
  return () => {
    console.log('beforeUnmount 执行')
  }
})

Reference

Type Declarations

export declare function useEffectOnce(effect: EffectCallback): void
export declare function useEffectOnce(effect: EffectCallback): void