Final Explain
Final Explain
React ek JavaScript library hai, jiska use hum UI (User Interface) banane ke liye karte hain.
Ismein hum components banate hain. Har component ek independent unit hota hai jisme
hum apna code likhte hain.
Aapka jo code hai, wo ek CRUD Application (Create, Read, Update, Delete) ka hai. Isme
hum users ka data add, update, delete, aur view kar sakte hain.
Code ka breakdown:
1. Constructor
js
Copy code
constructor(props) {
super(props);
this.state = {
users: [],
userName: '',
userFatherName: '',
userMotherName: '',
userEmail: '',
userDOB: '',
editId: null,
};
}
2. componentDidMount()
js
Copy code
componentDidMount() {
this.fetchUsers();
}
● Ye ek React lifecycle method hai, jo component mount hone ke baad chalti hai.
● Jab component pehli baar screen par dikhayi deta hai, ye method automatically chalti
hai aur fetchUsers() ko call karke users ka data fetch karne ka kaam karti hai.
3. fetchUsers()
js
Copy code
fetchUsers = () => {
fetch('https://676a3a7a863eaa5ac0ddb69d.mockapi.io/users')
.then((response) => response.json())
.then((data) => this.setState({ users: data }));
};
4. handleAddUser()
js
Copy code
handleAddUser = (values) => {
const { userName, userFatherName, userMotherName, userEmail,
userDOB } = values;
5. handleUpdateUser()
js
Copy code
handleUpdateUser = (values) => {
const { userName, userFatherName, userMotherName, userEmail,
userDOB, editId } = values;
fetch(`https://676a3a7a863eaa5ac0ddb69d.mockapi.io/users/${editId}`,
{
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(updatedUser),
})
.then((response) => response.json())
.then(() => {
this.fetchUsers();
});
}
};
6. delete()
js
Copy code
delete = (id) => {
fetch(`https://676a3a7a863eaa5ac0ddb69d.mockapi.io/users/${id}`,
{
method: 'DELETE',
}).then(() => {
this.fetchUsers();
});
};
7. render()
js
Copy code
render() {
const { users, editId } = this.state;
return (
<div className="container">
{/* Form section */}
<Formik
initialValues={...}
validate={...}
onSubmit={...}
>
{/* Formik form */}
</Formik>
Summary:
Umeed hai ab aapko code samajh aa gaya hoga! Agar koi aur confusion ho toh aap pooch
sakte hain.