Define a Layout component with a header and navigation bar.
This example assumes you're using functional components and React Router for navigation. If you haven't installed React Router yet, you can do so by running npm install react-router-dom.
// First, you need to import necessary hooks and components from 'react-router-dom'
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';
// Import your custom components
import HomeComponent from './HomeComponent';
import InputFormComponent from './InputFormComponent';
import DisplayComponent from './DisplayComponent';
import AboutComponent from './AboutComponent';
// This is your Layout component
function Layout() {
return (
<Router>
<div className="layout">
<header>
<h1>My React App</h1>
</header>
<nav>
<ul>
{/* These links should route to your different components */}
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/input">Input Form</Link>
</li>
<li>
<Link to="/display">Display</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
</ul>
</nav>
{/* A <Switch> looks through its children <Route>s and
renders the first one that matches the current URL. */}
<Switch>
<Route path="/about">
<AboutComponent />
</Route>
<Route path="/display">
<DisplayComponent />
</Route>
<Route path="/input">
<InputFormComponent />
</Route>
<Route path="/">
<HomeComponent />
</Route>
</Switch>
</div>
</Router>
);
}
export default Layout;
For this code to work properly, you must have the four components
(HomeComponent, InputFormComponent, DisplayComponent, and AboutComponent) defined and exported in their respective files.
Here's a simple CSS to get you started with the layout styling:
cssCopy code
.layout {
text-align: center;
}
header {
background-color: #333;
color: white;
padding: 10px 0;
}
nav ul {
list-style-type: none;
padding: 0;
}
nav ul li {
display: inline;
margin-right: 10px;
}
nav ul li a {
text-decoration: none;
color: #333;
}
/* Apply this class to the active link */
.active {
color: #555;
font-weight: bold;
}
In your CSS, make sure to design your active link appropriately, perhaps with a different color or font weight to indicate which page the user is currently viewing.
Remember, this is a functional, basic layout component. You would likely want to enhance the CSS with your own design system or use a framework like Tailwind CSS or Bootstrap to make it more aesthetically pleasing.