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

152
Visualizações
How to trigger function in react from foreignObject rendered with d3?

In React application I drag components

const Draggable = ({ children, onDragStart, onDragEnd }) => (
  <div
    className="draggable"
    draggable={true}
    onDragStart={onDragStart}
    onDragEnd={onDragEnd}
  >
    {children}
  </div>
);

when they dropped this components become svg elements rendered with d3js.

const onDrop = (e) => {
    e.stopPropagation();
    const newElement = {
      id: elements.length + 1,
      x: e.nativeEvent.offsetX,
      y: e.nativeEvent.offsetY,
      data: draggedData
    };
    setElements((elements) => [...elements, newElement]);
  };


 ...
    useEffect(() => {
        const svg = d3
          .select(svgRef.current)
          .attr("width", width)
          .attr("height", height);
    
        if (elements.length) {
          svg
            .selectAll(".node")
            .data(elements, (d) => d.id)
            .join(
              (enter) => {
                if (enter && prevCount !== elements.length) {
                  const { type } = enter.datum().data;
                  if (type === "person") {
                    renderNode(enter, svg, setElements, "enter");
                  } else {
                    renderInput(enter, svg, setElements, "enter");
                  }
...

One svg element contains foreignObject with input and one button (it would be more buttons).

node
      .append("foreignObject")
      .attr("width", labelWidth - labelPadding * 2)
      .attr("height", 22)
      .attr("x", labelPadding)
      .attr("y", labelPadding)
      .html((d) => {
        return `<div>
            <form>
              <input type='text' class='' />
              <button type='button' onclick='alert("The form was submitted")'>Ok</button>
            </form>
            <div class='circle'></div>
          </div>`;
      });

I want to trigger changes in state of react component when click on button inside svg. How can I handle this?

Here is working example

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