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

128
Vistas
Save components to a list and then conditionally render

I have a webpage that displays a list of buttons and corresponding iframe components. Each time a new button is clicked, a new iframe is displayed and the existing one is removed.

Here is the iframe component:

const IframeComp = ({ button }) => {

    return (
      <iframe src={button}></iframe>
    )
  }

export default IframeComp

I am loading all the possible iframes--as strings--into a list in app.jsx when the app loads like so:

  const [iframeComponents, setIframeComponents] = useState()
  useEffect(() => {
    function loadIframes() {
      let iframes = {}
      for (const iframeUrl of constants.buttonUrls) {
        const iframe = `<IframeComp button=${iframeUrl}></IframeComp>`
        iframes[iframeUrl] = iframe
      }
      setIframeComponents(iframes)
    }
    loadIframes()
  }, []);

And passing the iframeComponents to the iframe display page file which checks which button is pressed and renders the appropriate iframe. Of course, the iframes are getting rendered as strings right now. Is there a way to load/render these IframeComp components in the app.jsx as actual components/html, store them in a list, and then simply display them downstream based on button click?

Alternatively, I have tried using html-react-parser library to simply render the strings as html in the iframe display page file, but this doesn't work as it lowercases the strings, and they'd also have to take time to render on each button click.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Why not use component directly?

const [iframeComponents, setIframeComponents] = useState();

useEffect(() => {
  function loadIframes() {
    let iframes = {};
    for (const iframeUrl of constants.buttonUrls) {
      const iframe = <IframeComp button={iframeUrl}></IframeComp>;
      iframes[iframeUrl] = iframe;
    }
    setIframeComponents(iframes);
  }
  loadIframes();
}, []);

What is your actual code for change iframe by button?

over 4 years ago · Santiago Trujillo Denunciar

0

It's a bit hard to see what's going on here without seeing where you're rendering the iFrame but I'll point you where I think you might find what you're looking for:

Conditional rendering: https://reactjs.org/docs/conditional-rendering.html

Additionally you can actually use the .map function on an array in the render of a component like so:

function Comp() {
    return (
        <div>
            {['TestVal1', 'TestVal2'].map((val) => {return (<p>{val}</p>)})}
        </div>
    )
}
over 4 years ago · Santiago Trujillo 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