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

181
Visualizações
Cannot find name "offsetHeight" or "clientHeight" on a React/Next.js TypeScript project

I have a custom collapsible FAQ component that is based on the height of the element when it's expanded and vice versa.

import { useState, useRef, useEffect } from "react";

export default FAQItem({title, description}: FAQItemProps) {
  const [isOpen, setIsOpen] = useState(false);
  const heightRef = useRef<HTMLDivElement>(null);

  const collapsible = () => (!isOpen ? setIsOpen(true) : setIsOpen(false));
  
  useEffect(() => {
    !isOpen
      ? (heightRef.current!.style.height = "96px")
      : (heightRef.current!.style.height = `${offsetHeight}px`)
  }, [isOpen]);

  return(
    <div ref={heightRef}>
      <h2 onClick={collapsible}>{title}</h2>
      <p>{description}</p>
    </div>
  )
}

Because I'm dealing with TypeScript, some of the already answered solutions I could find unfortunately doesn't have anything TypeScript-related answered and won't fly with TS and had to add type definitions. And it returned with error Cannot find name "offsetHeight"., and clientHeight does the same thing too.

I've tried adding any to offsetHeight, including:

declare const window: Window & typeof globalThis & {
  offsetHeight: number | any;
}

...and as expected, the error went away but it didn't work - the height didn't change and and won't expand.

Nevertheless, all the solutions I could find unfortunately has no TypeScript-related answers regarding getting the width or height of an element on a React TypeScript project - all of them are just plain React questions without TypeScript, and I prefer to not use any external libraries of any kind either.

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

0

The reason you are getting the error is because you haven't defined the variables offsetHeight and clientHeight. Now, I know you don't need to define them, but because your usage is improper, the compiler thinks you forgot to create these variables.

Here's what you want: heightRef.current?.clientHeight and heightRef.current?.offsetHeight, the ? is just to avoid errors in case the ref is not loaded.

Edit with code

import "./styles.css";
import React from "react";

export default function App() {
  const [isOpen, setIsOpen] = React.useState(false);
  const heightRef = React.useRef<HTMLDivElement>(null);

  const collapsible = () => setIsOpen(!isOpen);

  console.log(isOpen, heightRef.current?.style?.height);

  React.useLayoutEffect(() => {
    if (isOpen) {
      heightRef.current?.style.height = `${heightRef.current?.offsetHeight}px`;
    } else {
      heightRef.current?.style.height = "96px";
    }
  }, [isOpen]);

  return (
    <div className="App" ref={heightRef} style={{height: 1100}}>
      <h1 onClick={collapsible}>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}

Please accept the answer if it helps! Thank you!

about 4 years ago · Juan Pablo Isaza Relatório

0

It seems like you haven't defined the offsetHeight variable yet referencing it here

(heightRef.current!.style.height = '${offsetHeight}px')  

it's why typescript is yelling. Although we don't need to define this, instead grab the value by doing this heightRef.current.offsetHeight .

Also instead of assigning like this heightRef.current!.style.height = "96px" do this (heightRef.current?.style.height = "96px"). As far as I know, there is no such operator !. in javascript.

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