React.js Portals and its usage
Portals help developers to render children into DOM nodes that exist outside the DOM hierarchy of the parent component.
ReactDOM.createPortal(child, container)
In this line of code the first argument, child is any renderable React child. It could be an element like a string, fragment. Second argument i.e the container is a DOM element. Normally when returning an element from the components method the element is mounted into the DOM as a child of the closest parent node. This is useful sometimes to insert a child into different locations in the DOM.
Here are some examples of the usage of portals. First one is HTML.
<!--Div for showing main component -->
<div id="example"></div>
<!--Div for popup showing -->
<div id="popup"></div>
In this HTML example we have two div tags. One is the main component and the other is for displaying popup messages.
In our next Javascript example two components are used. One component displays the main component and the other is used for displaying popups. In the main component there is a button,When we click on it the status will change from false to true and a popup will display. When the close button on the popup is clicked the status will change back to false.
//Here we are creating a component which will display in the form of the popups and we can also include some text messages on this model
class Pop extends React.Component {
constructor(props) {
super(props)
//Creating or capturing details of the div element
this.element = document.createElement('div')
}
//This function called automatically after rendering of the components
componentDidMount() {
//Appending of the element to the popup model
document.getElementById('popup').appendChild(this.element)
}
//This function unmount the displayed popups and this also et automatically called
componentWillUnmount() {
//removing the element popups from the appended previously
document.getElementById('popup').removeChild(this.element)
}
render() {
//Creating a portal for handling our cases
return ReactDOM.createPortal(
//Taking child and element
this.props.children,
this.element
)
}
}
The main components and second components contain child components of the pop-ups.
class Portalexample extends React.Component {
constructor(props) {
super(props)
//Initializing the model as the false which means the popups will be closed at the starting time and later according to the operations the value will be either true or false.
this.state = {showhideModel: false}
this.manageShowHide = this.manageShowHide.bind(this)
this.manageHide = this.manageHide.bind(this)
}
//Changing of the true and false of the show hide will be managed in this function and it will be called when clicked the button .
manageShowHide() {
this.setState({showhideModel: true})
}
manageHide() {
this.setState({showhideModel: false})
}
render() {
//Show and hide will be handles here on clicking the button
constshowpopups = this.state.showhideModel ? (
<Pop>
<div>
Hello friends <button onClick={this.manageHide}>Close</button>
</div>
</Pop>
) : ''
return (
<div>
The portal example <button onClick={this.manageShowHide}>Display Model</button>
{showpopups}
</div>
)
}
}
//Attaching the component with the div of html which we have created
ReactDOM.render(<Portalexample />, document.getElementById('example'))
robertfc rocker
09 Jul 2022
Leading Medical Payment Services supplier with 12+ years of experience in Multi-Specialty Medical Billing with a wide existence across the Country hospitalist billing services las vegas. Outsourced Medical Billing Services for Your Independent Method. Specialized Emphasis. Through Kareo's network of relied on invoicing firms. Medical Billing Services for Small Practices link Medical Billing Services for Small Practices Plus Negotiating - When it pertains to outstanding clinical payment support, rely on Barbara Young Medical Invoicing Solutions in Staten Island, NY. We are the leading Medical Billing Providers Outsourcing Firm certified and experienced to resolve varied medical specialties over 2 years now in the U.S.A.. Medical invoicing as well as coding solutions by SybridMD are not just one more payment remedy to contract out clinical invoicing as well as record patient data right into payment. A clinical invoicing service that won't nickel and penny you. When you pick AdvancedMD for taken care of billing, you pay an inexpensive and foreseeable. Firms that provide clinical billing services aid health care suppliers adjudicate cases and might give several other solutions.
Albina
25 Jul 2022
Thank you so much Love your blog.. no appointments needed
Albina
25 Jul 2022
This one is good. keep up the good work!.. melatonin gummies
Albina
25 Jul 2022
Great post I would like to thank you for the efforts you have made in writing this interesting and knowledgeable article. IV drip
Albina
25 Jul 2022
Thanks for the valuable information and insights you have so provided here... Knee replacement surgeon Kolkata
Albina
25 Jul 2022
Thank you for taking the time to publish this information very useful! Local moving company DC
Albina
25 Jul 2022
The post is written in very a good manner and it contains many useful information for me. casual plus size tops
Albina
25 Jul 2022
This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post! Ryan Bishti
Albina
25 Jul 2022
This was really an interesting topic and I kinda agree with what you have mentioned here! Juan Monteverde yacht
Albina
25 Jul 2022
Great write-up, I am a big believer in commenting on blogs to inform the blog writers know that they’ve added something worthwhile to the world wide web!.. commercial solar installer
Albina
25 Jul 2022
Great Information sharing .. I am very happy to read this article .. thanks for giving us go through info.Fantastic nice. I appreciate this post. Kim Anami reviews
Albina
25 Jul 2022
Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work! Nppe course
Albina
25 Jul 2022
useful information on topics that plenty are interested on for this wonderful post.Admiring the time and effort you put into your b!.. Omaha's native son
Albina
25 Jul 2022
Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. Will Kohler
Albina
25 Jul 2022
This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post.! andrew frame
Albina
25 Jul 2022
I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favorites blog site list and will be checking back soon. Please check out my site as well and let me know what you think. andrew frame
Albina
25 Jul 2022
Your blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing. Keep blogging.. andrew frame
Albina
25 Jul 2022
I'm glad to see the great detail here!. roy beck
Albina
25 Jul 2022
I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often. roy beck books
Albina
25 Jul 2022
Thank you for some other informative website. The place else may just I get that kind of information written in such a perfect method? I have a venture that I am simply now running on, and I’ve been at the glance out for such info. HillCo Partners
Albina
25 Jul 2022
I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. gino pozzo
Albina
25 Jul 2022
Your article has piqued a lot of positive interest. I can see why since you have done such a good job of making it interesting. aaa century appliance
Albina
25 Jul 2022
Thanks so much for sharing this awesome info! I am looking forward to see more postsby you! best garage door service near me
Albina
25 Jul 2022
I’ve been surfing online more than three hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. In my opinion, if all webmasters and bloggers made good content as you did, the web will be a lot more useful than ever before. Therapist near me
robertfc rocker
26 Jul 2022
Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place.. basic training for air force
robertfc rocker
26 Jul 2022
Please continue this great work and I look forward to more of your awesome blog posts. best willamette wine tours
robertfc rocker
28 Jul 2022
Thanks for the post and great tips..even I also think that hard work is the most important aspect of getting success.. San Diego SEO Company
Murphy
30 Jul 2022
Hi, I find reading this article a joy. It is extremely helpful and interesting and very much looking forward to reading more of your work.. Yoga studio near me
sam smith
16 Apr 2023
I just want the whole world to know about this spell caster I met two weeks ago, I cannot say everything he has done for me my wife left me 3 years ago left with my kids I was going through online when I meant this wonderful man's testimony online I decided to give it a try and my wife is back to me now and we are happily married again cause is too much to put in writing all I can say is thank you very much am very happy Contact him today on oseremenspelltemple@gmail.com or whatsapp him on +2348136482342. www.facebook.com/Dr-odion-spell-temple-110513923938220
Albona
08 Sep 2023
i am for the first time here. I found this board and I in finding It truly helpful & it helped me out a lot. I hope to present something back and help others such as you helped me. petnavy.com
Vicky
19 Sep 2023
I believe other website owners should take this website as an model, very clean and great user pleasant pattern . Packers and Movers Mumbai to Gurgaon
Albina
28 Aug 2024
Thanks for your insight for your fantastic posting. I’m glad I have taken the time to see this. barber shop vancouver
mtom
26 Sep 2024
I love how you write, it had me thinking. I??m going to bookmark this. chat.ib