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

134
Visualizações
SVG path from file in React component

Using Inkscape, I have generated svg files which contain paths that are identifiable by custom id. The objective is to implement a React component rendering one such svg file, styling one path given by id, and handing down onClick functionality to the styled path element. In my mind, there are two approaches.

First, one could load the svg file as a (mutable) data object and manipulate the path element directly. I do not want to do this.

Second, this could be achieved by layering the svg file in the background (ImageElement) with a styled svg element covering the original path element and another one providing event functionality. In order to "copy&paste" the given path from the background layer to the styled layer, I want to read the <d> attribute of the path from the svg file.

    renderSvgElement() {
        // from this.props.src get <d> attribute of path this.props.pathID
        ...
    }

    render () {
        const ImageElement = React.forwardRef((props, ref) => <img
                style={this.styles.img}
                src={this.props.src}
                ref={ref}
                onClick={this.imageClick.bind(this)}
            />);

        return (
            <div
                style={this.styles.container}
                ref={(node) => this.container = node}
            >
                <ImageElement ref={this.imgRef} />
                <svg
                    id="styled-layer"
                    ref={(node) => this.styledSvg = node}
                    style={this.styles.styledCanvas}
                >
                    {this.renderSvgElement()}
                </svg>
                <svg
                    id="onClick-layer"
                    ref={(node) => this.onClickSvg = node}
                    style={this.styles.onClickCanvas}
                >
                    {this.renderOnClickSvgElement()}
                </svg>
            </div>
        );

I have tried variations of

path = document.querySelector("...").
                contentDocument.querySelectorAll("path[path-id='...']")[0].getAttribute("d");

without success as the data object seems to be null on first render - at least the way I implemented it. Do I need to set the data object onComponentLoad / using React hooks, e.g. useEffect? Using the HTML DOM in such a way feels inelegant and not "reacty". Is there a better way?

about 4 years ago · Juan Pablo Isaza
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