unit4- ques
unit4- ques
1. How do you fetch data from a public API in a React application? Provide an example
using the Fetch API.
To fetch data from a public API in React, you can use the Fetch API along with React’s
useEffect and useState hooks.
● Steps:
useEffect(() => {
const fetchUsers = async () => {
try {
const response = await fetch('https://jsonplaceholder.typicode.com/users');
const data = await response.json();
setUsers(data);
} catch (error) {
console.error('Error fetching users:', error.message);
}
};
fetchUsers();
}, []);
return (
<div>
<h1>Users</h1>
<ul>
{users.map(user => (
<li key={user.id}>{user.name}</li>
))}
</ul>
</div>
);
};
2. Explain the role of useEffect in fetching data from a public API in React. How does it
ensure that the API is called only once when the component mounts?
Example:
useEffect(() => {
fetchData();
}, []); // Empty array = run only on mount
3. Describe a scenario where you might need to handle errors when fetching data from a
public API in React. How would you implement error handling in such a case?
● Scenario: If the API server is down or the URL is incorrect, you need to display an error
message to users.
● Error Handling: Use a try-catch block to catch errors and update a separate error
state.
Example:
const firebaseConfig = {
apiKey: 'YOUR_API_KEY',
authDomain: 'YOUR_AUTH_DOMAIN',
projectId: 'YOUR_PROJECT_ID',
};
firebase.initializeApp(firebaseConfig);
export const db = firebase.firestore();
2. How do you perform CRUD operations using Firebase Firestore in a React application?
1.
2.
3.
4.
Cloud Databases
1. Compare and contrast SQL and NoSQL databases. When would you choose to use
each type of database in a React application?
1.
Connect:
2.
3. Explain the concept of data consistency in cloud databases. How do SQL and NoSQL
databases handle data consistency differently?
● Data Consistency: Ensures that data remains accurate across database transactions.
● SQL: Strong consistency (data always consistent due to ACID rules).
● NoSQL: Eventual consistency (data updates propagate over time for better scalability).
General Questions
1. How do you decide which cloud database to use for a React application?
Factors to consider:
● Data Structure: Use SQL for structured data, NoSQL for flexibility.
● Scalability: NoSQL for scaling needs (e.g., large traffic).
● Complex Queries: SQL is better for complex relationships.
● Real-Time Needs: NoSQL like Firebase for real-time updates.
2. Explain the importance of data security when using cloud databases in a React
application.
3. Describe a scenario where you might need to use both SQL and NoSQL databases in a
single React application.
Integration: Use APIs to combine data from both databases into the React app.
___________________________________________________________
2. Instances: Virtual servers in the cloud used to run applications (e.g., EC2 in AWS).
3. Route Table: Defines rules for routing traffic within a Virtual Private Cloud (VPC).
4. Internet Gateway: A VPC component that allows communication between your VPC
resources and the internet.
5. Subnet: A segment of a VPC where you can group resources based on routing and
security needs.
6. Use of npm start: Runs the startup script defined in package.json to start a
development server for a React app.
7. Use of package.json: Contains metadata for the project, including dependencies and
scripts.
9. Difference Between Class and Function: Class components have lifecycle methods
and state; functional components are simpler and can use hooks for state and effects.
10. Amplify in AWS: A service for building, deploying, and hosting web and mobile
applications with backend support.
11. Use of Role in AWS: Assigns permissions to AWS resources to interact with other
services securely.
12. Use of AWS Bucket: Stores data, such as files or media, in Amazon S3.
14. Use of Arrow Function in React: Simplifies syntax and maintains the correct this
context in components.
15. Use of Security Group: Acts as a virtual firewall to control traffic to and from AWS
resources.
16. Use of VPC: Enables the creation of isolated networks for your AWS resources.
17. What is VM: A virtualized computing environment that mimics a physical computer.
18. What is Target Group: A collection of resources (e.g., EC2 instances) that receive
traffic routed by a load balancer.