LABASS2
LABASS2
SP22-BCS-171
WEB TECHNOLOGIES
LAB ASSIGNMENT #2
QUESTION 1:
CODE :
import React from 'react';
// User component
function User({ name }) {
return <h1>Hello, {name || "Guest"}</h1>;
}
// App component
function App() {
return (
<div>
<User name="MUHAMMAD MUJTABA" /> {/* Passing name prop */}
<User /> {/* No name prop, so defaults to "Guest" */}
</div>
);
}
QUESTION 2:
CODE :
import React, { useState } from 'react';
return (
<div>
<h1>Current Count: {count}</h1>
<button onClick={incrementCount}>Increment by {step}</button>
</div>
);
};
QUESTION 3:
CODE :
import React, { useEffect, useRef, useState } from 'react';
return (
<div>
<h1>Focus on the Input Field</h1>
<input
ref={inputRef} // Attach the ref to the input element
type="text"
value={inputValue}
onChange={handleChange}
placeholder="Type something..."
/>
<p>Input Value: {inputValue}</p>
</div>
);
};
export default FocusInputComponent;
SCREENSHOT:
QUESTION 4:
CODE :
import React from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
// Home component
function Home() {
return <h1>Welcome to the Home Page</h1>;
}
// About component
function About() {
return <h1>About Us</h1>;
}
// Contact component
function Contact() {
return <h1>Contact Us</h1>;
}