Unit IV Notes
Unit IV Notes
React
Overview
React applications often need to fetch data from external APIs to provide dynamic and
up-to-date content. This section will guide you through the process of fetching data from a public
API and displaying it in a React application.
https://jsonplaceholder.typicode.com/users
useEffect(() => {
fetchUserData();
}, []);
return (
<div>
<h1>Users</h1>
<ul>
{users.map((user) => (
<li key={user.id}>
<strong>Username:</strong> {user.username}, <strong>Full Name:</strong>
{user.name}, <strong>Email:</strong> {user.email}
</li>
))}
</ul>
</div>
);
Example Code
useEffect(() => {
fetchUserData();
}, []);
return (
<div>
<h1>Users</h1>
<ul>
{users.map((user) => (
<li key={user.id}>
<strong>Username:</strong> {user.username}, <strong>Full Name:</strong>
{user.name}, <strong>Email:</strong> {user.email}
</li>
))}
</ul>
</div>
);
};
Using Firebase with React: Setting Up Firebase in a React Project, CRUD Operations
Using Firebase Firestore
Overview
Firebase is a popular backend-as-a-service (BaaS) that provides various tools for building web
and mobile applications. This section will cover setting up Firebase in a React project and
performing CRUD operations using Firebase Firestore.
const firebaseConfig = {
apiKey: 'YOUR_API_KEY',
authDomain: 'YOUR_AUTH_DOMAIN',
projectId: 'YOUR_PROJECT_ID',
};
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
1. Create Data
- Use `addDoc` to create a new document in Firestore:
2. Read Data
- Use `getDocs` to read documents from Firestore:
3. Update Data
- Use `updateDoc` to update a document in Firestore:
4. Delete Data
- Use `deleteDoc` to delete a document from Firestore:
Example Code
const updateUser =
Cloud Databases: Types of Cloud Databases (SQL vs NoSQL), Connecting a React App to
a Cloud Database
Overview
Cloud databases offer a flexible and scalable way to manage data in web applications. This
section will cover the types of cloud databases, focusing on SQL and NoSQL databases, and
provide guidance on connecting a React app to a cloud database.
- Connect to MongoDB: Import the MongoDB driver and connect to your MongoDB cloud
database:
- Fetch Data: Use the MongoDB client to fetch data from your database:
fetchUserData();
Example Code
useEffect(() => {
connectToMongoDB();
fetchUserData();
}, []);
return (
<div>
<h1>Users</h1>
<ul>
{users.map((user) => (
<li key={user._id}>
<strong>Username:</strong> {user.username}, <strong>Full Name:</strong>
{user.fullName}, <strong>Email:</strong> {user.email}
</li>
))}
</ul>
</div>
);
};
Summary
- SQL databases are ideal for structured data and applications that require complex queries and
data consistency.
- NoSQL databases are suitable for unstructured or semi-structured data and applications that
require high scalability and flexibility.
- Connecting a React app to a cloud database involves choosing the appropriate database type
and using specific drivers or libraries to connect and interact with the database.
Sample Questions from Unit-IV :-
1. How do you fetch data from a public API in a React application? Provide an example using
the Fetch API.
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?
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?
1. What are the steps involved in setting up Firebase in a React project? Explain how to initialize
Firebase and connect to Firestore.
2. How do you perform CRUD operations using Firebase Firestore in a React application?
Provide examples for creating, reading, updating, and deleting documents.
3. Explain the benefits of using Firebase Authentication in a React application. How does it
simplify user authentication and authorization?
Cloud Databases
1. Compare and contrast SQL and NoSQL databases. When would you choose to use each
type of database in a React application?
2. Describe how to connect a React app to a MongoDB cloud database. What are the
necessary steps and libraries required for this connection?
3. Explain the concept of data consistency in cloud databases. How do SQL and NoSQL
databases handle data consistency differently?
General Questions
1. How do you decide which cloud database to use for a React application? What factors should
you consider when making this decision?
2. Explain the importance of data security when using cloud databases in a React application.
How can you ensure that your data is secure?
3. Describe a scenario where you might need to use both SQL and NoSQL databases in a
single React application. How would you integrate these databases?