Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

98
Visualizações
Listen to changes to element's content length or presence of a scrollbar in React

I have a container with overflow-y: auto and I need to shift the rest of my layout depending on whether its scrollbar is present or not.

I know I can check for scrollbar's presence at any given time:

const scrollbarVisible = (e) => e.scrollHeight > e.clientHeight

But I don't know how to listen/observe for the scrollbar's presence in order to trigger a re-render of my side-dock.

What are my options? Do I have any besides checking for the scrollbar in regular intervals with setInterval to detect the change that way?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could use the resize observer api to check for size changes in an element. An example of usage, also present in the link I mentioned, follows:

import * as React from 'react'
import useResizeObserver from '@react-hook/resize-observer'

const useSize = (target) => {
  const [size, setSize] = React.useState()

  React.useLayoutEffect(() => {
    setSize(target.current.getBoundingClientRect())
  }, [target])

  // Where the magic happens
  useResizeObserver(target, (entry) => setSize(entry.contentRect))
  return size
}

const App = () => {
  const target = React.useRef(null)
  const size = useSize(target)
  return (
    <pre ref={target}>
      {JSON.stringify({width: size.width, height: size.height}, null, 2)}
    </pre>
  )
}

After that you can check for the overflow as you did with JS

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda