EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

React Portals

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » React Native Tutorial » React Portals

React Portals

Introduction to React Portals

Many time when we are rendering any child components on any main components then it happens that child component gets overflow on the main component, due to which the layout of the application gets disturbed, so to deal with such type of situations we can use the concept of the portal. it allows us to insert directly a child component inside another show that it will stick to that component and will not get overflow. we can use the ReacDOM.createPortal (the child, Our container will be here), with this we will be able to handle overflow type of disturbance in the rendering of the components.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

We can see the simple syntax below where we are passing the child and the container. Here We have passed two attributes to the ReacDOM.createPortal function out of which first is the renderable child component. We can create a react Portal with the help of the below syntax.

ReacDOM.createPortal (the Child goes here, the  container goes here)

How do Portals Work in React?

Before going into a deeper analysis of the working of the portal we need to understand in which situations we need to use the concept of the portal. You have seen many times an overflow happens while displaying the component, it happens because the child components may not be inside the actual node of the component. with the help of the portal, we can directly insert the child component inside another component and it will handle the overflow condition. We pass the two attributes at the time of the creation of the portal, out of which the first is the child and second is the container. Here the first attribute which child is any renderable components (renderable means it can be rendered and can be visible to the end-users )

Examples of React Portals

Below we have written an example, this example contains three sections: the first section is the HTML section, the second is the JavaScript section and 3rd section is the CSS sections. let us understand each section for the implementation of the portal concept in the react js. In case if we want to run the below example we can create an HTML file and we can put html css and javascript all together with required dependency and we can run the examples or we can use some online react compilers and we can execute the below examples.

Example #1 – Html contents

Below is the HTML section, in this we have taken two div tags in which one is for the main component and another one is for displaying the popups with some message.

<!--Div for showing main component -->
<div id="example"></div>
<!--Div for popup showing -->
<div id="popup"></div>

Example #2 – Javascriptcontents

Below we have written the code for the react javascript, here we have written two components one component is for displaying the main component and another is used for displaying the popups. On the main component there will be a button, on clicking the button the status of the display will change from false to true and a popup will be visible and once we click to close on the open popups the status will again change to the false and the pop-ups will not be visible. We have initialized the display status for thee pop-ups as the false.

Popular Course in this category
React JS Redux Training (1 Course, 5 Projects)1 Online Courses | 5 Hands-on Projects | 18+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,487 ratings)
Course Price

View Course

Related Courses

Example #3 – First components, used for the popups

//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 component and second component or default components, this component contains the child component of 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'))

Example #4 – CSS Contents

Below is the css code for designing the popup which we will open on the clicking on the button of the main components.

#popup {
position: fixed;
z-index: 997;
}
/* popup designing with css is goes here */
#popup div {
background-color: green;
/*  Defining the height of the popups */
height: 99%;
/*  Defining the position of the popups */
position: fixed;
top: 0;
/*  Defining the display of the popups */
display: flex;
align-items: center;
left: 0;
/*  Defining the width of the popups *
width: 99%;
justify-content: center;
}

Images Explanation

In the below we have given two images of the screen out of them the first image is for the main components and the second image of the screen for the pop-up. The second image will appears when the display model button will be clicked and on clicking the close of the second image again the first image of the screen will appear.

React Portals-1.1

React Portals-1.2

Recommended Articles

This is a guide to React Portals. Here we also discuss the introduction and how do portals work in react along with its different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. React Native Authentication
  2. React Router Transition
  3. React Component Library
  4. React Redux Connect

React JS Redux Training (1 Course, 5 Projects)

1 Online Courses

5 Hands-on Projects

18+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

2 Shares
Share
Tweet
Share
Primary Sidebar
React Native Tutorial
  • Basic
    • What is React Native?
    • React Versions
    • React Constructor
    • React Native Architecture
    • React Native Libraries
    • React Components Libraries
    • React Native Components
    • React Component Library
    • React Component Lifecycle
    • React Native Framework
    • React Higher Order Component
    • React Ternary Operator
    • React Native Charts
    • React Native Layout
    • React Native Grid
    • React Native Fetch
    • React Native Modal
    • React Native SVG
    • Button in React Native
    • React List Components
    • React Native Element
    • React Native FlatList
    • React Native SectionList
    • react native dropdown
    • React Native Menu
    • React Native State
    • React State Management
    • React Native Tabs
    • React Native Tab Bar
    • React Format
    • React-Native Switch
    • React Native Firebase
    • React Native Flexbox
    • React Native StatusBar
    • React Native ScrollView
    • React Native ListView
    • React Native TextInput
    • React Native Table
    • React-Native Border Style
    • React Native Search Bar
    • React-Native StyleSheet
    • React Native Vector Icons
    • React Native Login Screen
    • React Native Splash Screen
    • React Native Image Picker
    • React Native Navigation
    • React Native Swift
    • React Controlled Input
    • React Fragment
    • React Native Color
    • React Portals
    • React Refs
    • React shouldComponentUpdate()
    • React ComponentDidMount()
    • React componentWillUpdate()
    • React Native Orientation
    • React Native Animation
    • React Native Form
    • React Props
    • React Native Linear Gradient
    • React Native AsyncStorage
    • React Error Boundaries
    • React Native Progress Bar
    • React-Native Calendar
    • React Native Linking
    • React Native DatePicker
    • React Native Image
    • React Native Drawer
    • React Native Drawer Navigation
    • React Native Fonts
    • React Native Overlay
    • React Native OneSignal
    • React Native Template
    • React Native Router
    • React Router Transition
    • React Dispatcher
    • React Native Redux
    • React JSX
    • React native keyboardavoidingview
    • React Native Permissions
    • React Redux Connect
    • React Native Material
    • React Native Gesture Handler
    • React Native Theme
    • React Native Accessibility
    • React Native Justify Content
    • MobX React Native
    • React Native Authentication
    • React Native UUID
    • React Native Geolocation
    • React Native Debugger
    • React Native Carousel
    • React Native Local Storage
    • React Native TypeScript
    • React Bootstrap
    • React Native SQLite
    • React Native Interview Questions
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - React JS Redux Training (1 Course, 5 Projects) Learn More