Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

93
Vistas
How do I reference an element that is to be returned by a React component?

I want to start out by telling you that I am new to react and I come from a background of working almost entirely with vanilla JS and HTML. I want to know how to reference/pass an element as an argument in a function similar to how you would go about it in JS:

const myElement = document.getElementById("my-element");

or

const myElement = document.createElement("div");
function testFunc(element) {
return element.getBoundingClientRect();
}
testFunc(myElement);

I have googled a bit but haven't found any good answers, all I could find was about "ref" but I am sure there is a simpler solution.

This is what I have in React now:

import React from "react";
import "./myComp.css";

function myComp(props) {
  const sliderContainer = (
    <div className="slider-container" style={props.styles}></div>
  );

  function myFunc(element) {
      return element.getBoundingClientRect();
  }

  const elementDimensions = myFunc(sliderContainer);

  return { sliderContainer };
}

export default myComp;

But what i want to do is:

import React from "react";
import "./myComp.css";

function myComp(props) {
  const sliderContainer = "slider-container" //SOME SORT OF REFRENCE / SELECTOR, I'VE FIGURED OUT THAT querySelectors IS NOT THE RIGHT APPORACH

  function myFunc(element) {
      return element.getBoundingClientRect();
  }

  const elementDimensions = myFunc(sliderContainer);

  return (
    <div className="slider-container" style={props.styles}>
        <div className="myChild"></div>
    </div>
  );
}

export default myComp;
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

This should work:

const sliderContainer = useRef(null);

function myFunc(element) {
  return element.getBoundingClientRect();
}

useEffect(() => {
  const elementDimensions = myFunc(sliderContainer.current);
});

return (
  <div ref={sliderContainer} className="slider-container" style={props.styles}>
      <div className="myChild"></div>
  </div>
);

The ref property will assign the element to sliderContainer.current once the component has been mounted.

Note that the value is initially set to be null: useRef(null).

You must wait until the element is not null before accessing it as the element will not yet be mounted when the component is initially rendered.

about 4 years ago · Juan Pablo Isaza Denunciar

0

in React you can use

document.getElementById('id-object');

or

document.querySelector('object-class');

But you can find more info in this article on Medium.

Hope that this was useful!

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use document.getElementById('slider-container'); or any other use of document in React.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda