Skip to content

Commit

Permalink
Add hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinBirkhoff committed Jul 19, 2023
1 parent e054d48 commit 8b8b162
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/hooks/src/useMount/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useEffect } from 'react'

const useMount = (fn: () => void) => {
useEffect(() => {
fn?.()
}, [])
}

export default useMount
13 changes: 13 additions & 0 deletions packages/hooks/src/useUnmount/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useEffect } from 'react'
import useLatest from '../useLatest'
const useUnmount = (fn: () => void) => {
const fnRef = useLatest(fn)
useEffect(
() => () => {
fnRef.current()
},
[]
)
}

export default useUnmount
14 changes: 14 additions & 0 deletions packages/hooks/src/useUnmountedRef/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useEffect, useRef } from 'react'

const useUnmountedRef = () => {
const unmountedRef = useRef(false)
useEffect(() => {
unmountedRef.current = false
return () => {
unmountedRef.current = true
}
}, [])
return unmountedRef
}

export default useUnmountedRef

0 comments on commit 8b8b162

Please sign in to comment.