MERN Manual-Reactprograms
MERN Manual-Reactprograms
render() {
return (
<div>
<h1>
Hello
{this.state.hello}
</h1>
</div>
);
}
}
o/p:
PROGRAM 2
Demonstrate React Component Life Cycle-Mounting phase--static
getDerivedStateFromProps method
import React, { Component } from "react";
import ReactDOM from "react-dom/client";
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 className='App'>
<h1>Counter App</h1>
<label>
Initial Count:
<input
type="number"
value={this.state.initialCount}
onChange={this.handleInitialCountChange}
/>
</label>
<Counter initialCount={this.state.initialCount} />
</div>
);
}
}
PROGRAM 3
Demonstrate React Component Life Cycle-Mounting phase -render() method:
// Filename - index.js
PROGRAM 4
Demonstrate React Component Life Cycle-Mounting phase-componentDidMount()
Function
// Filename - index.js
PROGRAM 5
Demonstrate React Component Life Cycle-updation phase --setState() method
// Filename - index.js
increment = () => {
this.setState((prevState) => ({
count: prevState.count + 1,
}));
};
decrement = () => {
this.setState((prevState) => ({
count: prevState.count - 1,
}));
};
render() {
return (
<div>
<h1>
The current count is :{" "}
{this.state.count}
</h1>
<button onClick={this.increment}>
Increase
</button>
<button onClick={this.decrement}>
Decrease
</button>
</div>
);
}
}
PROGRAM 6
Demonstrate React Component Life Cycle-Unmounting phase --
componentWillUnmount() method
render() {
return <h1>Hello sahyadri</h1>;
}
}
render() {
let comp;
if (this.state.display) {
comp = <ComponentOne />;
}
return (
<div>
{comp}
<button onClick={this.delete}>
Delete the component
</button>
</div>
);
}
}
PROGRAM 7
Develop a React Component for basic front-end application-registration form
// Filename - src/index.js:
// Filename - src/App.js:
import './App.css';
import Form from "./Form"
function App() {
return (
<div className="App">
<Form />
</div>
);
}
return (
<div className="form">
<div>
<h1>User Registration</h1>
</div>
<form>
{/* Labels and inputs for form data */}
<label className="label">Name</label>
<input
onChange={handleName}
className="input"
value={name}
type="text"
/>
<br></br>
<label className="label">Email</label>
<input
onChange={handleEmail}
className="input"
value={email}
type="email"
/>
<br></br>
<label className="label">Password</label>
<input
onChange={handlePassword}
className="input"
value={password}
type="password"
/>
<br></br>
<button onClick={handleSubmit} className="btn" type="submit">
Submit
</button>
</form>
</div>
);
}
/* Filename: App.css */
.App {
text-align: center;
background-color: green;
}
.label {
display: block;
font-size: larger;
color: white;
padding: 5px;
}
.input {
font-size: larger;
padding: 5px;
margin: 2px;
.btn {
color: white;
background-color: red;
border-radius: 5px;
font-size: larger;
display: block;
padding: 5px;
margin: 10px auto;
}
.messages {
display: flex;
justify-content: center;
}
.error {
display: block;
background-color: red;
color: white;
width: fit-content;
height: 50px;
padding: 5px;
}
.success {
display: block;
background-color: lightblue;
color: black;
width: fit-content;
height: 50px;
padding: 5px;
}