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

114
Visualizações
Find Color of Text Element in React

I have a Link component and I want to find out what the color of the text is. How would I do that in React?

I know with Vanilla JS, you can find it with element.style.color however if I perform similar with below code using textEl.style.color I get undefined.

export function TextLink({ children, path, inline = false, dash = false }: TextLinkProps) {
  const textEl = useRef("");
  return (
    <StyledLink href={path} inline={inline} dash={dash} ref={textEl}>
      {children}
    </StyledLink>
  );
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use window.getComputedStyle() and pass the element itself by its ref like so:


const ref = React.useRef();
...
...
...

React.useEffect(() => {
  const style = window.getComputedStyle(ref.current)
  console.log(style.color); // rgb(255, 0, 0) 
}, []);

Window.getComputedStyle()

about 4 years ago · Juan Pablo Isaza Relatório

0

You are forgetting the 'current' with your reference. write it like this :

textEl.current.style.color
about 4 years ago · Juan Pablo Isaza Relatório

0

Gives rgb(0, 0, 0) to me

const textEl = React.useRef(null);

  React.useEffect(() => {
    if (textEl.current) {
      console.log(window.getComputedStyle(textEl.current).color)
    }
  }, [textEl])

Or simply just use textEl.current.style.color if you don't need computed one

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