Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
Agregar elemento a un elemento existente Reaccionar

Estoy creando una encuesta con SurveyJs. Al hacerlo, busco crear un botón para agregar a cada título de pregunta. Pude hacer lo siguiente con Javascript, pero busco hacer esencialmente lo mismo con React.

La representación de la encuesta funciona bien, así que no creo que valga la pena mostrarla. La siguiente es la función de devolución de llamada cuando se procesa cada pregunta. Lo siguiente funciona bien:

 const onAfterRenderQuestion = (survey, options) = { // create the button const btn = document.createElement("button") btn.innerHTML = "Click me!" // define what the button should do btn.onClick = function () { // do whatever } // get the question title from the survey and append the button to the end of it let header = options.htmlElement.querySelector("h5") if (!header) header = options.htmlElement header.appendChild(btn) }

El código anterior funciona bien y, como puede ver, está usando Javascript. Sin embargo, estoy tratando de hacer exactamente lo mismo usando React pero no puedo hacerlo. De momento tengo esto:

 // create the button component const myButtonElement = ( <Button onClick={() => {*call some function*}} ) let header = options.htmlElement.querySelector("h5") if (!header) header = options.htmlElement // create the entire component const questionTitle = React.createElement("div", {}, header) const buttonElement = React.createElement("div", {}, myButtonElement) const titleWithButton = React.createElement("div", {}, [questionTitle, buttonElement]) ReactDOM.render(titleWithButton, document.getElementById(options.question.id))

Sin embargo, el código anterior da como resultado el siguiente error:

Error: los objetos no son válidos como hijos de React (encontrado: [objeto HTMLHeadingElement]). Si tenía la intención de representar una colección de niños, use una matriz en su lugar.

Incluso si uno no está familiarizado con Surveyjs, ¿cómo puedo simplemente agregar un elemento a un elemento existente usando React, de manera similar a como ya lo hice con Javascript?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Es porque el encabezado es un elemento HTML, mientras que reaccionar espera que el tercer argumento sea un elemento de reacción.

 let header = options.htmlElement.querySelector("h5") if (!header) header = options.htmlElement const questionTitle = React.createElement("div", dangerouslySetInnerHTML: {__html: `<h5>${header.innerHTML}</h5>` })

HTML escrito dentro de JSX no son componentes HTML nativos. Estos son desarrollados por el equipo de reacción para imitar HTML y es un componente de reacción.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!