React JS Experiment
React JS Experiment
js
Sr. No. Experiment Name List
To create a simple React.js application that displays a
1 welcome message using JSX and demonstrates React
component-based architecture.
To compare JavaScript and JSX by creating UI elements
2
using both approaches in a React app.
To demonstrate how to pass data (props) to a React
3
functional component using JSX.
To create a functional React component and apply CSS
4
styling to it.
To demonstrate the use of state in a React functional
5
component by creating a counter application.
To pass dynamic data from a parent component to a child
6
component using props and validate it using PropTypes.
To create a controlled component in React where the input
7
field is controlled using the state.
To implement basic routing in a React application using
8
BrowserRouter.
To implement nested routes, allowing sub-pages within a
9
main page.
10 To restrict access to a route unless the user is authenticated.
Experiment 1: Creating a Simple React Component with
JSX
Aim: To create a simple React.js application that displays a welcome message using JSX and demonstrates
React component-based architecture.
Objective:
Theory:
Code Implementation:
function Greeting() {
return <h1>Hello, Welcome to React with JSX!</h1>;
}
function App() {
return (
<div>
<Greeting />
<p>This is a JSX-based React component.</p>
</div>
);
}
npm start
Results:
Objective:
Theory:
Code Implementation:
function App() {
return (
<div>
{jsElement}
{jsxElement}
</div>
);
}
npm start
Results:
Successfully demonstrated how React elements are created using both JavaScript and JSX.
JSX provides a cleaner and more readable way to define UI elements.
Experiment 3: Creating a Functional Component with Props in JSX
Aim: To demonstrate how to pass data (props) to a React functional component using JSX.
Objective:
Theory:
Props (Properties) allow components to receive data and render dynamic content.
JSX expressions can embed JavaScript inside curly braces {}.
Code Implementation:
function Welcome(props) {
return <h1>Welcome, {props.name}!</h1>;
}
function App() {
return (
<div>
<Welcome name="Alice" />
<Welcome name="Bob" />
<Welcome name="Charlie" />
</div>
);
}
npm start
Welcome, Alice!
Welcome, Bob!
Welcome, Charlie!
Results:
Objective:
Theory:
Code Implementation:
Inside the src folder, create a new file Greeting.js and add the following code:
function Greeting() {
return <h1 className="greeting-text">Hello, Welcome to React!</h1>;
}
.greeting-text {
color: blue;
font-size: 24px;
text-align: center;
margin-top: 20px;
}
Result:
Objective:
Theory:
Code Implementation:
function Counter() {
// Declare state
const [count, setCount] = useState(0);
return (
<div style={{ textAlign: "center", marginTop: "20px" }}>
<h1>Counter: {count}</h1>
<button onClick={() => setCount(count + 1)}>Increase</button>
<button onClick={() => setCount(count - 1)}>Decrease</button>
<button onClick={() => setCount(0)}>Reset</button>
</div>
);
}
function App() {
return <Counter />;
}
Result:
Objective:
Theory:
Code Implementation:
// Validate Props
User.propTypes = {
name: PropTypes.string.isRequired,
age: PropTypes.number.isRequired,
};
// Default Props
User.defaultProps = {
name: "Guest",
age: 18,
};
export default User;
function App() {
return (
<div>
<User name="Alice" age={25} />
<User name="Bob" age={30} />
<User /> {/* Uses default props */}
</div>
);
}
Result:
Objective:
Theory:
In a controlled component, form inputs are controlled by React state, not by the DOM.
The value of the input is tied to the state, and onChange updates the state.
Controlled components allow better control over user inputs.
Code Implementation:
jsx
CopyEdit
import React, { useState } from "react";
function NameForm() {
// Declare state to store input value
const [name, setName] = useState("");
return (
<div style={{ textAlign: "center", marginTop: "20px" }}>
<h1>Controlled Component Example</h1>
<input
type="text"
placeholder="Enter your name"
value={name} // Controlled by state
onChange={(e) => setName(e.target.value)} // Updates state
style={{ padding: "10px", fontSize: "16px", marginBottom: "10px" }}
/>
<p>Your Name: {name}</p>
</div>
);
}
function App() {
return <NameForm />;
}
npm start
3. Open http://localhost:3000/ in a browser.
4. Type your name into the input field and see it dynamically update below.
Result:
Successfully created a controlled component where React controls the input field.
The displayed text updates in real-time as the user types.
Experiment 8: Basic Routing in React
Aim: To implement basic routing in a React application using BrowserRouter.
Objective:
Theory:
React Router allows navigation between different components without refreshing the page.
<BrowserRouter> enables clean URLs.
<Route> defines which component to load based on the URL.
<Link> provides navigation without reloading the page.
Code Implementation:
Modify src/App.js:
function App() {
return (
<Router>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
<li><Link to="/contact">Contact</Link></li>
</ul>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact />} />
</Routes>
</Router>
);
}
export default App;
Home.js
About.js
Contact.js
npm start
Result:
Objective:
Theory:
Code Implementation:
function App() {
return (
<Router>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/dashboard">Dashboard</Link></li>
</ul>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/dashboard/*" element={<Dashboard />} />
</Routes>
</Router>
);
}
function Dashboard() {
return (
<div>
<h1>Dashboard</h1>
<nav>
<ul>
<li><Link to="profile">Profile</Link></li>
<li><Link to="settings">Settings</Link></li>
</ul>
</nav>
<Outlet />
<Routes>
<Route path="profile" element={<Profile />} />
<Route path="settings" element={<Settings />} />
</Routes>
</div>
);
}
Profile.js
Settings.js
npm start
Result:
Objective:
1. Implement a protected route that only allows access if a user is logged in.
2. Redirect users to the login page if they are not authenticated.
Theory:
Code Implementation:
const Auth = {
isAuthenticated: false,
login(cb) {
Auth.isAuthenticated = true;
cb();
},
logout(cb) {
Auth.isAuthenticated = false;
cb();
},
};
Create ProtectedRoute.js:
function App() {
return (
<Router>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/dashboard">Dashboard</Link></li>
</ul>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/login" element={<Login />} />
<Route path="/dashboard" element={
<ProtectedRoute>
<Dashboard />
</ProtectedRoute>
} />
</Routes>
</Router>
);
}
function Login() {
const navigate = useNavigate();
function handleLogin() {
Auth.login(() => navigate("/dashboard"));
}
return (
<div>
<h2>Login Page</h2>
<button onClick={handleLogin}>Login</button>
</div>
);
}
Result: