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

168
Visualizações
Is there a way to determine the current font-size of a DOM element in ReactJS?

In our app, a piece of text changes font-size using a CSS class with responsive breakpoints.

But some users want fine-tooth control over the font-size, so we have "increase" and "decrease" font size buttons.

Using React, I have:

const [characterSize, setCharacterSize] = useState(45);

and

function increaseCharacterSize() {
  setCharacterSize(characterSize + 1);
}
<div
  style={{ fontSize: `${characterSize}px` }}
  className="myClass"
  >Our awesome text</div>

Calling increaseCharacterSize increase the font size as expected.

The problem is, I don't want to set the characterSize to "45" as per the example, because the starting size (from which to start incrementing/decrementing from) is dependent on the current fontSize of the element in the DOM (which is based on the CSS class which is based on the browser window).

I need to be able to determine the DOM element's current rendered fontSize. In plain vanilla javascript, this would look something like:

window.getComputedStyle(el, null).getPropertyValue('font-size');

Is there any way to do this in React?

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

0

Get the ref and call your desired function:

export default function App() {
  const inputRef = useRef();

  useEffect(() => {
    const size = window
      .getComputedStyle(inputRef.current, null)
      .getPropertyValue("font-size");

    console.log(size);
  }, []);

  return (
    <div className="App">
      <div ref={inputRef} style={{ fontSize: 45 }}>
        Our awesome text
      </div>
    </div>
  );
}

https://codesandbox.io/s/cranky-tharp-ldexxr?file=/src/App.js:67-439

about 4 years ago · Juan Pablo Isaza Relatório

0

I think there are two parts to this:

  1. as @super claims, you can simply use vanilla javascript in react.

  2. What is the reason you want to take control over the font size in your app's code?

Normally you would do this by using rem. It is a size unit you can use in your CSS that will automatically scale in size as the user changes the font size setting in the browser. So basically this is a wheel that already exists and you want to re-invent.

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