React application using multiple functional components
In this example, we will show you how to create an application with multiple functional components using React Hooks. We have already explained a similar example using class components but here we will use only functional components.
Checkout more articles on ReactJS
Demo Application
Package Version
File Structure
- react-functional-app
- node_modules
- public
- index.html
- src
- pages
- Content.js
- Footer.js
- Header.js
- Navbar.js
- Sidebar.js
- App.css
- App.js
- index.css
- index.js
- pages
- package-lock.json
- package.json
- README.md
Steps to create a react application using functional components
- Create a react application
- Create separate components
- Add multiple components in single component
- Output
1. Create a react application
Create a react application using create-react-app
npm package. Run the following command to create a react app.
1 | npx create-react-app react-functional-app |
2. Create separate components
We will split our layout into five different components listed below.
- Header Component
- Navbar Component
- Sidebar Component
- Content Component
- Footer Component
Refer to the file structure mentioned earlier to create components.
1 2 3 4 5 6 7 8 9 10 | const Header = () => { return ( <div className="header"> <h1>Demo Application</h1> <p>Demo application created in React App</p> </div> ) } export default Header; |
1 2 3 4 5 6 7 8 9 10 11 | const Navbar = () => { return ( <div className="navbar"> <a href="/#">Home</a> <a href="/#">About</a> <a href="/#" className="right">Contact</a> </div> ) } export default Navbar; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | const Sidebar = () => { return ( <div className="side"> <h2>Arcu bibendum</h2> <h5>Sit amet mattis vulputate</h5> <div className="fakeimg" style={{ height: 200 }}>Image</div> <p>Non blandit massa enim nec dui nunc mattis enim. Egestas tellus rutrum tellus pellentesque eu tincidunt tortor aliquam nulla..</p> <h3>Massa enim</h3> <p>Lorem ipsum dolor sit ame.</p> <div className="fakeimg" style={{ height: 60 }}>Image</div><br /> <div className="fakeimg" style={{ height: 60 }}>Image</div><br /> <div className="fakeimg" style={{ height: 60 }}>Image</div> </div> ) } export default Sidebar; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | const Content = () => { return ( <div className="main"> <h2>Lorem ipsum dolor</h2> <h5>quam pellentesque, Dec 10, 2018</h5> <div className="fakeimg" style={{ height: 200 }}>Image</div> <p>Nisi vitae suscipit..</p> <p>Semper quis lectus nulla at. Nullam ac tortor vitae purus faucibus ornare suspendisse. Nunc faucibus a pellentesque sit. Risus quis varius quam quisque id diam vel quam elementum. Ornare aenean euismod elementum nisi quis eleifend quam.</p> <br /> <h2>Placerat vestibulum</h2> <h5>elementum integer enim neque, Sep 21, 2018</h5> <div className="fakeimg" style={{ height: 200 }}>Image</div> <p>Bibendum est ultricies..</p> <p>Semper quis lectus nulla at. Nullam ac tortor vitae purus faucibus ornare suspendisse. Nunc faucibus a pellentesque sit. Risus quis varius quam quisque id diam vel quam elementum.</p> </div> ) } export default Content; |
1 2 3 4 5 6 7 8 9 | const Footer = () => { return ( <div className="footer"> <h2>Footer</h2> </div> ) } export default Footer; |
To apply style for application, we will add below style in App.css file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | * { box-sizing: border-box; } /* Style the body */ body { font-family: Arial, Helvetica, sans-serif; margin: 0; } /* Header/logo Title */ .header { padding: 80px; text-align: center; background: #269faf; color: white; } /* Increase the font size of the heading */ .header h1 { font-size: 40px; } /* Style the top navigation bar */ .navbar { overflow: hidden; background-color: #333; } /* Style the navigation bar links */ .navbar a { float: left; display: block; color: white; text-align: center; padding: 14px 20px; text-decoration: none; } /* Right-aligned link */ .navbar a.right { float: right; } /* Change color on hover */ .navbar a:hover { background-color: #ddd; color: black; } /* Column container */ .row { display: -ms-flexbox; /* IE10 */ display: flex; -ms-flex-wrap: wrap; /* IE10 */ flex-wrap: wrap; } /* Create two unequal columns that sits next to each other */ /* Sidebar/left column */ .side { -ms-flex: 30%; /* IE10 */ flex: 30%; background-color: #f1f1f1; padding: 20px; } /* Main column */ .main { -ms-flex: 70%; /* IE10 */ flex: 70%; background-color: white; padding: 20px; } /* Fake image, just for this example */ .fakeimg { background-color: #aaa; width: 100%; padding: 20px; } /* Footer */ .footer { padding: 20px; text-align: center; background: #ddd; } |
3. Add multiple components in single component
Import all components in App.js
file and add it in below format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import React from 'react'; import './App.css'; import Header from './pages/Header'; import Navbar from './pages/Navbar'; import Content from './pages/Content'; import Sidebar from './pages/Sidebar'; import Footer from './pages/Footer'; function App() { return ( <div> <Header /> <Navbar /> <div className="row"> <Content /> <Sidebar /> </div> <Footer /> </div> ); } export default App; |
4. Output
That’s all you need to write. Run the application and check the output in the browser.
That’s it for today.
Thank you for reading. Happy Coding..!! 🙂