Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

javascript - Failure to present different components using React.portal on a single div

I got a "section" component which renders all of the items I would like to collapse on click, the content which should be inside of that collapsed item is in a different component, the way I tried to render those different components(on section click) is by React.createportal. But something is wrong, I can grab the right name out of the clicked section but when I insert it inside the div's "id" attribute nothing happends, I am getting a blank div collapsing on click. the section code:

{sections.map((section, index) => {
            return (
                <div className="container" onClick={handleClick} key={index}>
                  <Icon icon= {section.icon}/>
                  <SectionTitle content={section.content}/>
                  {isClicked && section.name === sectionName && <div id={sectionName}></div>}
                </div>
            )
        })}

And the content components code:

export default function About() {
return ReactDOM.createPortal(
    <div className="about-section">
      <p>
          About works!
      </p>  
    </div>,
    document.getElementById("about")
)

}

As you can see "about" component expects a div's id value of "about" and that's exactly what happens when the "about" section is clicked, so I do not know what goes wrong, please help ?

question from:https://stackoverflow.com/questions/65599101/failure-to-present-different-components-using-react-portal-on-a-single-div

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...