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

165
Visualizações
React / React Router - Treat <a> tags like <Link> tags

I am passing pre-compiled HTML to my React component through props. The original data looks something like this.

let pageData = [
   title  : 'About',
   content: '<p>Hello <strong> World!</p><a href="/contact">Contact</a>'
]

Then in the component using the dangerouslySetInnerHTML method because I'm not sure if there is anything else to use.

import React from 'react';

export default class PostText extends React.Component {
  render() {
    return (
        <div
          dangerouslySetInnerHTML={{__html: this.props.content}}
        />
      </div>
    );
  }
}

But my question is, how can I convert the <a href="/contact">Contact</a> tag to <Link to="/contact">Contact</Link>.

Even if I don't change the mark up, is there a way to to do something like, "when I click an <a> tag, treat is as <Link> tag".

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I think you could do something like this, intercepting links click events inside you div and navigating with router:

<div
    dangerouslySetInnerHTML={{__html: this.props.content}}
    onClick={event => this.navigate(event)}
  />
</div>

and then:

navigate (event) {
  event.preventDefault()

  if (event.target.tagName === 'A') {
    this.props.router.push(event.target.getAttribute('href'))
  }
}
over 4 years ago · Santiago Trujillo 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