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

226
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
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