React write something outside of root element, so we can use React portals.
public/index.html : after root div add one more div with id name is : 'root-test-portal'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<div id="root-test-portal"></div> // added this div with id here.
</body>
</html>
App.js file code
import './App.css';
import TestPage from './Pages/TestPage';
import TestReactPortals from './Pages/TestReactPortals';
function App() {
return (
<div className="App">
<TestPage />
<TestReactPortals />
</div>
);
}
export default App;
/pages/TestReactPortals.js file code
import React, { Component } from "react";
import ReactDom from 'react-dom';
class TestReactPortals extends Component {
render() {
return ReactDom.createPortal(
<h5>Test React Portals...</h5>,
document.getElementById('root-test-portal')
);
};
}
export default TestReactPortals;
Pages/TestPage.js file code
import React from "react";
const TestPage = () => {
return (
<>
<h2>Test page</h2>
</>
);
}
export default TestPage;
No comments:
Post a Comment