简体   繁体   English

如何将 innerHTML 用于 Accordion (PrimeReact)?

[英]How to use innerHTML for Accordion (PrimeReact)?

In my React project, I am using the PrimeReact library to display some Accordions.在我的 React 项目中,我使用 PrimeReact 库来显示一些手风琴。 But I need to create them dynamically and this is the current result:但我需要动态创建它们,这是当前结果:

在此处输入图像描述

As you see, the html tags are also shown in the answers of the faqs.如您所见,html 标签也显示在常见问题的答案中。 I tried using innerHTML and dangerouslySet... but to no success.我尝试使用innerHTMLdangerouslySet...但没有成功。

Here is my relevant code:这是我的相关代码:

createAccordions = () => {
    const allFAQs = this.state.allFAQs;
    let accordions = [];

    for (const faq of allFAQs) {
        const accordion = <AccordionTab header={faq.question} dangerouslySetInnerHTML={{__html: `<p>yo man</p>`}}></AccordionTab>;
        accordions.push(accordion);
    }

    return accordions
}

render() {
    return (
        <div className="component">
            <h1 style={{textAlign: 'center'}}>{this.state.pageTitle}</h1>

            <div className="p-grid">
                
                <div className="p-col-3">

                </div>

                <div className="p-col-6">
                    <Accordion>
                        {this.createAccordions()}
                    </Accordion>
                </div>


                <div className="p-col-3">
                    
                </div>
            </div>
        </div>
    )
}

How can I properly set the innerhtml for the accordions?如何正确设置手风琴的 innerhtml?

I took a look at the source and found that the library tries to render the children you pass to it.我看了一下源代码,发现图书馆试图渲染你传递给它的孩子。 I see you tried dangerouslySetInnerHTML , but set it on the AccordionTab itself instead of the child passed into it.我看到你试过dangerouslySetInnerHTML ,但是将它设置在AccordionTab本身而不是传递给它的孩子。

I set up a quick demo on CodePen.我在 CodePen 上设置了一个快速演示 I passed in a div container to set the innerHTML on, and it seems to work!我传入了一个div容器来设置 innerHTML,它似乎可以工作!

<AccordionTab header="Demo">
  <div dangerouslySetInnerHTML={{ __html: "<ul><li>I am HTML</li></ul>" }}></div>
</AccordionTab>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM