// 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;
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;
}
jsxCopy code
import './App.css';
jsxCopy code
import React, { useState } from 'react';
import './App.css'; // This imports the CSS you just saved
import Home from './Home'; // Assume Home.js exists
// ... rest of your imports
import React from 'react';
function HomeComponent() {
// Inline styles
const styles = {
container: {
padding: '50px',
textAlign: 'center',
},
header: {
fontSize: '2em',
color: '#333',
},
paragraph: {
fontSize: '1em',
color: '#666',
},
highlight: {
color: '#0056b3',
}
};
return (
<div style={styles.container}>
<h1 style={styles.header}>Welcome to the Space Exploration App</h1>
<p style={styles.paragraph}>
Explore the Galaxy in this Space Exploration Simulation.
</p>
<p style={{...styles.paragraph, ...styles.highlight}}>
Navigate through the simulation drills of Star Fleet Academy's Entrance Qualification Test for new Space Cadets
</p>
</div>
);
}
export default HomeComponent;
cssCopy code
/* HomeComponent.css */
.home-container {
padding: 50px;
text-align: center;
}
.home-header {
font-size: 2em;
color: #333;
}
.home-paragraph {
font-size: 1em;
color: #666;
}
.home-highlight {
color: #0056b3;
}
jsxCopy code
import React from 'react';
import './HomeComponent.css';
function HomeComponent() {
return (
<div className="home-container">
<h1 className="home-header">Welcome to My React App</h1>
<p className="home-paragraph">
This is a simple React application that demonstrates the core concepts of React.js, such as components, state, and props.
Navigate through the app to learn more about its features. Enjoy exploring!
</p>
<p className="home-paragraph home-highlight">
Start by navigating to the Input Form to enter some data!
</p>
</div>
);
}
export default HomeComponent;
jsxCopy code
import React, { useState } from 'react';
function InputForm({ onSubmit }) {
const [inputData, setInputData] = useState({
name: '',
age: ''
});
const [errors, setErrors] = useState({});
// Validates the form and returns a boolean indicating if the data is valid
const validateForm = () => {
let valid = true;
let errors = {};
if (!inputData.name) {
errors.name = 'Name is required';
valid = false;
}
if (!inputData.age || isNaN(inputData.age)) {
errors.age = 'Please enter a valid age';
valid = false;
}
setErrors(errors);
return valid;
};
// Handles form submission
const handleSubmit = (event) => {
event.preventDefault();
if (validateForm()) {
onSubmit(inputData); // This would be a function passed down from the parent component
setInputData({ name: '', age: '' }); // Reset form data
setErrors({}); // Reset errors
}
};
// Updates the inputData state as the user types in the inputs
const handleChange = (event) => {
const { name, value } = event.target;
setInputData({
...inputData,
[name]: value
});
};
return (
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="name">Name:</label>
<input
type="text"
id="name"
name="name"
value={inputData.name}
onChange={handleChange}
/>
{errors.name && <div style={{ color: 'red' }}>{errors.name}</div>}
</div>
<div>
<label htmlFor="age">Age:</label>
<input
type="number"
id="age"
name="age"
value={inputData.age}
onChange={handleChange}
/>
{errors.age && <div style={{ color: 'red' }}>{errors.age}</div>}
</div>
<button type="submit">Submit</button>
</form>
);
}
export default InputForm;
jsxCopy code
import React from 'react';
function Display({ data }) {
if (!data) {
return <div>No data to display.</div>;
}
return (
<div>
<h3>Processed Data:</h3>
<p><strong>Name:</strong> {data.name}</p>
<p><strong>Age:</strong> {data.age}</p>
</div>
);
}
export default Display;
jsxCopy code
import React, { useState } from 'react';
import InputForm from './InputForm'; // Assume InputForm is in the same directory
import Display from './Display'; // Assume Display is in the same directory
function ParentComponent() {
const [data, setData] = useState(null);
const handleFormSubmit = (formData) => {
setData(formData);
};
return (
<div>
<InputForm onSubmit={handleFormSubmit} />
<Display data={data} />
</div>
);
}
export default ParentComponent;
import React from 'react';
function About() {
return (
<div style={{ padding: '20px', maxWidth: '600px', margin: 'auto' }}>