one-hook
Size
0.76 kb
View source

useInView

Track the visibility of an element.

Installation

npm install @1hook/use-in-view

Usage

import {  } from '@1hook/use-in-view'

Track the visibility of an element:

function () {
  const { ,  } = ()

  return < ={}>...</>
}

Set a default value until the target is observed:

function () {
  const { ,  = true } = ()

  return < ={}>...</>
}

Customize the intersection observer:

function () {
  const { ,  } = ({
    : .('#root'),
    : '10px',
    : 0.5,
  })

  return < ={}>...</>
}

Observe once:

function () {
  const { ,  } = ({
    : () =>  && (),
  })

  return < ={}>...</>
}

API Reference

UseInViewOptions

Options for the useInView hook.

PropTypeDefault
onChange?
((inView: boolean) => void)
-
trackState?
boolean
true
autoObserve?
boolean
true
threshold?
number | number[]
-
rootMargin?
string
-
root?
Element | Document | null
-

UseInViewReturn

Return value for the useInView hook.

PropTypeDefault
inView
boolean | undefined
-
target
Element | null
-
ref
Function
-
unobserve
() => void
-
observe
() => void
-

On this page