Component Creation Lab
Component Creation Lab
Component Creation:
Create a functional component called Header that renders a header for a webpage.
function Header() {
return (
<header>
</header>
);
Create a class component called Counter that displays a count and increments it when a button is
clicked.
handleIncrement = () => {
this.setState((prevState) => ({
count: prevState.count + 1,
}));
};
render() {
return (
<div>
<p>Count: {this.state.count}</p>
<button onClick={this.handleIncrement}>Increment</button>
</div>
);
}
}
State Management:
Convert the Counter component from exercise 1 into a class component and manage the count using
state.
handleIncrement = () => {
this.setState((prevState) => ({
count: prevState.count + 1,
}));
};
handleReset = () => {
this.setState({
count: 0,
});
};
render() {
return (
<div>
<p>Count: {this.state.count}</p>
<button onClick={this.handleIncrement}>Increment</button>
<button onClick={this.handleReset}>Reset</button>
</div>
);
}
}
Props:
Create a functional component called Greeting that takes a prop name and displays a personalized
greeting.
Use the Greeting component in another component and pass a name prop to it.
// App.js
return (
<div>
<Greeting name={userName} /> {/* Use the Greeting component */}
</div>
);
};
Event Handling:
Create a functional component called Button that renders a button.
Add an event handler to the button that displays an alert when clicked.
Conditional
function Button() {
alert('Button clicked!');
};
return (
);
Rendering:
Create a functional component called ConditionalDisplay that renders different content based on a
boolean prop isVisible.
If isVisible is true, display a message saying "Component is visible". Otherwise, display "Component is
hidden".
return (
<div>
{isVisible ? (
<p>Component is visible</p>
):(
<p>Component is hidden</p>
)}
</div>
);
};
Forms:
Create a class component called LoginForm that contains input fields for username and password.
Add a submit button to the form that logs the entered username and password to the console.
return (
<form onSubmit={this.handleSubmit}>
<div>
<label htmlFor="username">Username:</label>
<input
type="text"
id="username"
name="username"
value={username}
onChange={this.handleInputChange}
/>
</div>
<div>
<label htmlFor="password">Password:</label>
<input
type="password"
id="password"
name="password"
value={password}
onChange={this.handleInputChange}
/>
</div>
<button type="submit">Submit</button>
</form>
);
}
}
Fetching Data:
Create a class component called QuoteGenerator that fetches a random quote from an API when
mounted.
componentDidMount() {
this.fetchRandomQuote();
}
fetchRandomQuote = () => {
axios
.get('https://api.adviceslip.com/advice')
.then((response) => {
const { advice } = response.data.slip;
this.setState({ quote: advice });
})
.catch((error) => {
console.log('Error fetching quote:', error);
});
};
render() {
const { quote } = this.state;
return (
<div className="quote-container">
<h1>Random Quote Generator</h1>
<p>{quote}</p>
<button onClick={this.fetchRandomQuote}>Get New Quote</button>
</div>
);
}
}
export default QuoteGenerator;
Component Composition:
Create a parent component called App that contains child components Header, Counter, and Greeting.
};
constructor(props) {
super(props);
this.state = {
count: 0,
};
handleIncrement = () => {
this.setState((prevState) => ({
count: prevState.count + 1,
}));
};
render() {
return (
<div>
<p>Count: {this.state.count}</p>
<button onClick={this.handleIncrement}>Increment</button>
</div>
);
};
render() {
return (
<div>
<Header />
<Counter />
<Greeting />
</div>
);