From cea01f4aac4366e619c210d2ae1125bc9ac04a35 Mon Sep 17 00:00:00 2001 From: Sumanth U <157620444+Sumanth077s@users.noreply.github.com> Date: Sat, 16 Nov 2024 19:35:16 +0530 Subject: [PATCH] Update App.js --- 17-random-person/final/src/App.js | 79 +++++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 19 deletions(-) diff --git a/17-random-person/final/src/App.js b/17-random-person/final/src/App.js index 9a1471115..6b99717ef 100644 --- a/17-random-person/final/src/App.js +++ b/17-random-person/final/src/App.js @@ -1,4 +1,7 @@ +// Import React and necessary hooks import React, { useState, useEffect } from 'react' + +// Import icons from react-icons library import { FaEnvelopeOpen, FaUser, @@ -7,17 +10,33 @@ import { FaPhone, FaLock, } from 'react-icons/fa' + +// API URL to fetch random user data const url = 'https://randomuser.me/api/' + +// Default image to display when no user image is available const defaultImage = 'https://randomuser.me/api/portraits/men/75.jpg' + function App() { + // State to manage loading status const [loading, setLoading] = useState(true) + + // State to store user data const [person, setPerson] = useState(null) + + // State to display the value of the selected attribute (e.g., name, email) const [value, setValue] = useState('random person') + + // State to display the title of the selected attribute const [title, setTitle] = useState('name') + + // Function to fetch a random user from the API const getPerson = async () => { - setLoading(true) - const response = await fetch(url) - const data = await response.json() + setLoading(true) // Set loading to true before fetching data + const response = await fetch(url) // Fetch data from the API + const data = await response.json() // Parse the response JSON + + // Extract relevant details from the API response const person = data.results[0] const { phone, email } = person const { large: image } = person.picture @@ -30,6 +49,7 @@ function App() { street: { number, name }, } = person.location + // Create a new object with the formatted user data const newPerson = { image, phone, @@ -39,76 +59,97 @@ function App() { street: `${number} ${name}`, name: `${first} ${last}`, } + + // Update state with the new user data setPerson(newPerson) - setLoading(false) - setTitle('name') - setValue(newPerson.name) + setLoading(false) // Set loading to false after data is fetched + setTitle('name') // Default title to 'name' + setValue(newPerson.name) // Default value to user's name } + // useEffect runs once when the component is mounted useEffect(() => { - getPerson() + getPerson() // Fetch initial random user }, []) + + // Event handler to update displayed value and title when hovering over icons const handleValue = (e) => { if (e.target.classList.contains('icon')) { - const newValue = e.target.dataset.label - setTitle(newValue) - setValue(person[newValue]) + const newValue = e.target.dataset.label // Get the label from the data attribute + setTitle(newValue) // Update the title + setValue(person[newValue]) // Update the value with the corresponding user detail } } + + // Render the UI return (
+ {/* Background block for visual styling */}
+ + {/* Main content block */}
+ {/* User image */} random user + + {/* Display title and value of the selected attribute */}

My {title} is

{value}

+ + {/* Icons for different attributes */}
-
+ + {/* Button to fetch a new random user */}