๐๐๐ฒ ๐๐ of #100DaysOfCode
- ๐ผ๐๐
๐๐๐๐๐ ๐ซ๐๐๐๐๐๐๐๐ ๐ผ๐๐๐๐
๐๐๐๐
๐ฉ๐๐ฐ๐
๐จ๐๐
๐ผ๐๐
๐๐๐
-
When you need to update a document, Mongoose provides several methods, one of the most popular being ๐๐๐๐
๐ฉ๐๐ฐ๐
๐จ๐๐
๐ผ๐๐
๐๐๐
. This function finds a document by its _๐ถ๐ฑ
(the unique id generated by MongoDB) and updates it with new data.
๐๐ก๐ฒ ๐ฌ๐ก๐จ๐ฎ๐ฅ๐ ๐ฒ๐จ๐ฎ ๐ฎ๐ฌ๐ ๐ญ๐ก๐ข๐ฌ?
๐ง๐ช๐ฏ๐ฅ๐๐บ๐๐ฅ๐๐ฏ๐ฅ๐๐ฑ๐ฅ๐ข๐ต๐ฆ
simplifies the update process by combining both finding and updating into one operation. Itโs efficient and easy to use, especially when dealing with documents that have a unique identifier.
๐๐๐ญ๐ญ๐ข๐ง๐ ๐ญ๐ก๐ ๐๐จ๐๐ฎ๐ฆ๐๐ง๐ญ ๐๐:
Before you can update a document, you need to know its unique identifier (_๐๐
). You can retrieve the _๐๐
of a document by querying your database with a method like ๐ง๐ช๐ฏ๐ฅ๐๐ฏ๐ฆ
or ๐ง๐ช๐ฏ๐ฅ
, depending on your needs.
๐๐ฑ๐ฉ๐ฅ๐๐ง๐๐ญ๐ข๐จ๐ง ๐จ๐ ๐ญ๐ก๐ ๐๐ญ๐ญ๐๐๐ก๐๐ ๐ฌ๐ง๐ข๐ฉ๐ฉ๐๐ญ:
-๐
๐ข๐ง๐ ๐ญ๐ก๐ ๐๐จ๐๐ฎ๐ฆ๐๐ง๐ญ: First, findOne
is used to locate a user by their email address. If a user is found, their _id
is accessed from the returned document.
-๐๐ฉ๐๐๐ญ๐ ๐ญ๐ก๐ ๐๐จ๐๐ฎ๐ฆ๐๐ง๐ญ: With the _id
in hand, findByIdAndUpdate
is called to update the userโs email address. The { new: true }
option returns the updated document instead of the original.
This method of first finding the document to retrieve the _id
and then using that _id
for updating is a common pattern in Mongoose.
2 Likes
๐๐๐ฒ ๐๐ of #100DaysOfCode
- ๐ซ๐๐๐๐๐๐๐๐๐๐๐ ๐ด๐๐๐๐๐ซ๐ฉ ๐ถ๐๐๐๐๐๐๐๐ -
When working with MongoDB, youโll often encounter operators that start with a $
symbol. These operators are special keywords used in queries, updates, and aggregations, allowing you to perform powerful and complex operations on your data.
Letโs break down some of the most commonly used $
operators and see them in action!
-
$๐ฌ๐๐ญ
The $๐ฌ๐๐ญ
operator is used to update the value of a field in a document. If the field does not exist, it will be added.
-
$๐ข๐ง๐
The $๐ข๐ง๐
operator increments the value of a field by a specified amount. Itโs useful for counters, stock management, or any numeric updates.
-
$๐ฉ๐ฎ๐ฌ๐ก
The $๐ฉ๐ฎ๐ฌ๐ก
operator adds an element to an array field. If the array does not exist, it will be created.
-
$๐ ๐ญ๐
and $๐ฅ๐ญ๐
These operators are used for comparison queries. $๐ ๐ญ๐
stands for โgreater than or equal to,โ and $๐ฅ๐ญ๐
stands for โless than or equal to.โ They are often used together to filter documents within a range.
-
$๐จ๐ซ
The $๐จ๐ซ
operator is used to perform logical OR operations on an array of conditions. It returns documents that match any of the given conditions.
-
$๐ข๐ง
The $๐ข๐ง
operator allows you to match documents where the value of a field equals any value in a specified array.
These $
operators are just a few examples of the power and flexibility MongoDB provides for working with data. Whether youโre updating documents, filtering queries, or managing arrays, understanding these operators will help you unlock the full potential of MongoDB.
2 Likes
๐๐๐ฒ ๐๐ of #100DaysOfCode
- ๐ด๐๐๐๐๐๐๐ ๐บ๐๐๐๐๐ ๐ฏ๐๐๐๐ -
Schema hooks, also known as middleware, are functions that can be executed at different stages of a documentโs lifecycle. They can be categorized into three types:
- ๐๐ซ๐ ๐๐ข๐๐๐ฅ๐๐ฐ๐๐ซ๐: Runs before a specific operation.
- ๐๐จ๐ฌ๐ญ ๐๐ข๐๐๐ฅ๐๐ฐ๐๐ซ๐: Runs after a specific operation.
- ๐๐ซ๐ซ๐จ๐ซ ๐๐๐ง๐๐ฅ๐ข๐ง๐ ๐๐ข๐๐๐ฅ๐๐ฐ๐๐ซ๐: Handles errors that occur during pre or post middleware.
๐ญ. ๐ฃ๐ฟ๐ฒ ๐ ๐ถ๐ฑ๐ฑ๐น๐ฒ๐๐ฎ๐ฟ๐ฒ:
Pre middleware functions execute before a specified operation, such as saving a document. They are perfect for tasks like validation or data manipulation.
๐ฎ. ๐ฃ๐ผ๐๐ ๐ ๐ถ๐ฑ๐ฑ๐น๐ฒ๐๐ฎ๐ฟ๐ฒ:
Post middleware functions execute after a specified operation. They are useful for tasks like logging or performing additional actions after a document has been saved.
๐ฏ. ๐๐ฟ๐ฟ๐ผ๐ฟ ๐๐ฎ๐ป๐ฑ๐น๐ถ๐ป๐ด ๐ ๐ถ๐ฑ๐ฑ๐น๐ฒ๐๐ฎ๐ฟ๐ฒ:
Error handling middleware can be used to catch and handle errors that occur during the execution of pre or post middleware.
๐๐๐ฎ ๐๐จ๐ ๐๐๐๐๐ข๐ ๐๐ค๐ค๐ ๐จ?
- ๐๐๐ฅ๐ข๐๐๐ญ๐ข๐จ๐ง: Perform custom validations before saving a document.
- ๐๐จ๐ ๐ ๐ข๐ง๐ : Track changes or actions performed on documents.
- ๐๐๐ญ๐ ๐๐ซ๐๐ง๐ฌ๐๐จ๐ซ๐ฆ๐๐ญ๐ข๐จ๐ง: Modify data before or after saving it to the database.
- ๐๐ซ๐ซ๐จ๐ซ ๐๐๐ง๐๐ฅ๐ข๐ง๐ : Manage and handle errors gracefully.
Schema hooks are a powerful feature of Mongoose that can help you implement complex logic and maintain clean code. By incorporating hooks into your application, you can ensure that your data operations are smooth and your application logic is well-organized.
2 Likes
๐๐๐ฒ ๐๐ of #100DaysOfCode
- ๐ฌ๐๐๐๐๐๐.๐๐ -
Express.js is a fast, unopinionated, and minimalist web framework for Node.js. It provides a robust set of features for web and mobile applications, making it easier to build dynamic websites and APIs. Express.js handles everything from simple routing to complex middleware functions, giving developers the tools to build scalable, high-performance web applications.
๐ช๐ต๐ ๐จ๐๐ฒ ๐๐
๐ฝ๐ฟ๐ฒ๐๐.๐ท๐?
- ๐๐ข๐ฆ๐ฉ๐ฅ๐ข๐๐ข๐ญ๐ฒ: Express abstracts away the complexities of setting up a server with Node.js, allowing you to focus on the logic of your application.
- ๐
๐ฅ๐๐ฑ๐ข๐๐ข๐ฅ๐ข๐ญ๐ฒ: With a minimalistic approach, Express allows you to structure your application however you see fit, without imposing specific patterns.
- ๐๐ข๐๐๐ฅ๐๐ฐ๐๐ซ๐ ๐๐ฎ๐ฉ๐ฉ๐จ๐ซ๐ญ: Express has extensive support for middleware, making it easy to add additional functionality to your application.
- ๐๐จ๐ฆ๐ฆ๐ฎ๐ง๐ข๐ญ๐ฒ & ๐๐๐จ๐ฌ๐ฒ๐ฌ๐ญ๐๐ฆ: A large community and vast ecosystem of plugins and middleware make it easy to extend the functionality of your app.
Whether youโre building a simple website or a complex RESTful API, Express.js is the go-to framework for Node.js developers.
2 Likes
๐๐๐ฒ ๐๐ of #100DaysOfCode
- ๐ด๐๐
๐
๐๐๐๐๐๐๐ ๐๐ ๐ฌ๐๐๐๐๐๐.๐๐ -
Middleware is a fundamental concept in Express.js that allows you to control the flow of requests and responses in your application. Middleware functions are executed sequentially and can modify the request or response objects, end the request-response cycle, or pass control to the next middleware function.
๐๐ก๐๐ญ ๐ข๐ฌ ๐๐ข๐๐๐ฅ๐๐ฐ๐๐ซ๐?
In Express.js, middleware is essentially a function that sits between the request and response objects. It can be used to execute code, modify the request or response objects, end the request-response cycle, or call the next middleware in the stack.
๐ง๐๐ฝ๐ฒ๐ ๐ผ๐ณ ๐ ๐ถ๐ฑ๐ฑ๐น๐ฒ๐๐ฎ๐ฟ๐ฒ:
- ๐๐ฉ๐ฉ๐ฅ๐ข๐๐๐ญ๐ข๐จ๐ง-๐๐๐ฏ๐๐ฅ ๐๐ข๐๐๐ฅ๐๐ฐ๐๐ซ๐:
- Bound to the Express app using
app.use()
or app.METHOD()
.
- Can apply to all routes or specific ones.
- ๐๐จ๐ฎ๐ญ๐๐ซ-๐๐๐ฏ๐๐ฅ ๐๐ข๐๐๐ฅ๐๐ฐ๐๐ซ๐:
- Works similarly to application-level middleware but is bound to an instance of
express.Router()
.
- ๐๐ซ๐ซ๐จ๐ซ-๐๐๐ง๐๐ฅ๐ข๐ง๐ ๐๐ข๐๐๐ฅ๐๐ฐ๐๐ซ๐:
- Specifically designed to handle errors in your application.
- ๐๐ฎ๐ข๐ฅ๐ญ-๐ข๐ง ๐๐ข๐๐๐ฅ๐๐ฐ๐๐ซ๐:
- Express.js provides built-in middleware for common tasks like serving static files (
express.static
), parsing JSON (express.json
), and more.
- ๐๐ก๐ข๐ซ๐-๐๐๐ซ๐ญ๐ฒ ๐๐ข๐๐๐ฅ๐๐ฐ๐๐ซ๐:
- Middleware developed by others that you can integrate into your Express app. Examples include
morgan
for logging and passport
for authentication.
๐ช๐ต๐ ๐ ๐ถ๐ฑ๐ฑ๐น๐ฒ๐๐ฎ๐ฟ๐ฒ ๐ถ๐ ๐๐บ๐ฝ๐ผ๐ฟ๐๐ฎ๐ป๐:
โ ๐๐จ๐๐ฎ๐ฅ๐๐ซ๐ข๐ญ๐ฒ: Break down your applicationโs functionality into smaller, manageable pieces.
โ ๐๐๐ฎ๐ฌ๐๐๐ข๐ฅ๐ข๐ญ๐ฒ: Middleware can be reused across different routes or even different projects.
โ ๐๐จ๐ง๐ญ๐ซ๐จ๐ฅ: Gain fine-grained control over the request-response cycle.
Middleware is a powerful feature in Express.js, enabling you to build scalable, maintainable applications with ease. By leveraging middleware effectively, you can handle everything from logging and authentication to error handling and data parsing.
2 Likes
๐๐๐ฒ ๐๐ of #100DaysOfCode
- ๐๐๐.๐๐๐ ๐๐ ๐ฌ๐๐๐๐๐๐.๐๐ -
The ๐ข๐ฑ๐ฑ.๐ถ๐ด๐ฆ()
function in Express.js is a powerful and versatile method used to apply middleware to your application. Middleware functions are executed sequentially and can perform a variety of tasks, such as modifying the request and response objects, handling errors, or terminating the request-response cycle.
๐๐ผ๐ ๐๐ผ๐ฒ๐ ๐ฎ๐ฝ๐ฝ.๐๐๐ฒ()
๐ช๐ผ๐ฟ๐ธ?
When you call ๐ข๐ฑ๐ฑ.๐ถ๐ด๐ฆ()
, youโre telling Express to use a specific middleware function. This middleware can apply to all routes, specific routes, or even a group of routes.
๐ช๐ต๐ ๐จ๐๐ฒ ๐ฎ๐ฝ๐ฝ.๐๐๐ฒ()
?
โ ๐
๐ฅ๐๐ฑ๐ข๐๐ข๐ฅ๐ข๐ญ๐ฒ: Apply middleware to specific routes or globally.
โ ๐๐จ๐๐ฎ๐ฅ๐๐ซ๐ข๐ญ๐ฒ: Break down your applicationโs logic into reusable pieces.
โ ๐๐จ๐ง๐ญ๐ซ๐จ๐ฅ: Manipulate the request/response objects or handle errors in a centralized manner.
Using app.use()
effectively can greatly enhance the maintainability and scalability of your Express applications.
3 Likes
๐๐๐ฒ ๐๐ of #100DaysOfCode
- ๐ฌ๐๐๐๐๐๐.๐๐ ๐น๐๐๐๐๐๐ -
Routers in Express.js are like mini-apps, allowing you to organize your routes and middleware into modular, manageable chunks. With routers, you can create modular, mountable route handlers, making it easier to build and maintain complex web applications.
๐ช๐ต๐ ๐จ๐๐ฒ ๐ฅ๐ผ๐๐๐ฒ๐ฟ๐?
โ ๐๐ซ๐ ๐๐ง๐ข๐ณ๐๐ญ๐ข๐จ๐ง: Keep your codebase clean and organized by grouping related routes together.
โ ๐๐จ๐๐ฎ๐ฅ๐๐ซ๐ข๐ญ๐ฒ: Break down your application into smaller, self-contained modules.
โ ๐๐๐ฎ๐ฌ๐๐๐ข๐ฅ๐ข๐ญ๐ฒ: Reuse routers across different parts of your application.
๐๐ฟ๐ฒ๐ฎ๐๐ถ๐ป๐ด ๐ฎ ๐ฅ๐ผ๐๐๐ฒ๐ฟ:
To create a router, you use express.Router()
and define your routes and middleware as you would on the main app.
In the attached snippet, the router handles requests to /router
and its sub-routes like /router/about
.
๐๐ฒ๐ป๐ฒ๐ณ๐ถ๐๐ ๐ผ๐ณ ๐จ๐๐ถ๐ป๐ด ๐ฅ๐ผ๐๐๐ฒ๐ฟ๐:
โ ๐๐จ๐๐ ๐๐ฅ๐๐ซ๐ข๐ญ๐ฒ: Each router focuses on a specific set of routes or features, making the code easier to understand and maintain.
โ ๐๐๐ฉ๐๐ซ๐๐ญ๐ข๐จ๐ง ๐จ๐ ๐๐จ๐ง๐๐๐ซ๐ง๐ฌ: You can isolate different parts of your application, reducing the likelihood of code conflicts.
Routers are an essential tool in building scalable Express.js applications, especially as your project grows in size and complexity.
2 Likes
๐๐๐ฒ ๐๐ of #100DaysOfCode
- ๐ฉ๐๐๐๐
๐๐๐ ๐จ๐ท๐ฐ ๐น๐๐๐๐๐ ๐๐ ๐ฌ๐๐๐๐๐๐.๐๐ -
API routes in Express.js allow you to create endpoints that interact with data, perform operations, and return responses in JSON or other formats. They are the backbone of any web API, enabling client applications to communicate with your server.
๐ช๐ต๐ฎ๐ ๐๐ฟ๐ฒ ๐๐ฃ๐ ๐ฅ๐ผ๐๐๐ฒ๐?
API routes are URL endpoints that respond to HTTP requests (GET, POST, PUT, DELETE, etc.) and are typically used to perform CRUD (Create, Read, Update, Delete) operations on resources.
In the attached snippet, the API routes handle requests to manage a simple user resource. The routes allow clients to create, retrieve, update, and delete users.
๐ช๐ต๐ ๐จ๐๐ฒ ๐๐ฃ๐ ๐ฅ๐ผ๐๐๐ฒ๐?
โ ๐๐๐ฉ๐๐ซ๐๐ญ๐ข๐จ๐ง ๐จ๐ ๐๐จ๐ง๐๐๐ซ๐ง๐ฌ: API routes focus on data handling and logic, separate from the presentation layer.
โ ๐๐จ๐ง๐ฌ๐ข๐ฌ๐ญ๐๐ง๐๐ฒ: Follow RESTful principles to create predictable and standard API endpoints.
โ ๐๐๐๐ฅ๐๐๐ข๐ฅ๐ข๐ญ๐ฒ: As your application grows, API routes can be easily extended to support additional resources and operations.
API routes are crucial for any web service, enabling communication between the server and clients, such as web apps, mobile apps, or other servers.
3 Likes
๐๐๐ฒ ๐๐ of #100DaysOfCode
- ๐ฐ๐๐๐๐๐
๐๐๐๐๐๐ ๐๐ ๐น๐๐๐๐ -
React is a popular JavaScript library for building user interfaces, primarily for single-page applications (SPAs). Developed by Facebook, React allows developers to create large web applications that can update and render efficiently in response to data changes. The key feature of React is its component-based architecture, which promotes reusability and makes it easier to manage complex UIs.
๐ช๐ต๐ ๐จ๐๐ฒ ๐ฅ๐ฒ๐ฎ๐ฐ๐?
- ๐๐จ๐ฆ๐ฉ๐จ๐ง๐๐ง๐ญ-๐๐๐ฌ๐๐: Break your UI into reusable components, making your code easier to manage and maintain.
- ๐๐ข๐ซ๐ญ๐ฎ๐๐ฅ ๐๐๐: Reactโs virtual DOM improves performance by updating only the parts of the DOM that need to change.
- ๐๐ง๐ข๐๐ข๐ซ๐๐๐ญ๐ข๐จ๐ง๐๐ฅ ๐๐๐ญ๐ ๐
๐ฅ๐จ๐ฐ: React enforces a one-way data flow, making the data logic in your application more predictable and easier to debug.
4 Likes
๐๐๐ฒ ๐๐ of #100DaysOfCode
- ๐น๐๐๐๐ ๐ช๐๐๐๐๐๐๐๐๐ -
Components are the building blocks of a React application. Each component is a self-contained unit of code that can be reused throughout your application. Components can be class-based or functional, and they accept inputs called props to render dynamic content.
๐ง๐๐ฝ๐ฒ๐ ๐ผ๐ณ ๐๐ผ๐บ๐ฝ๐ผ๐ป๐ฒ๐ป๐๐:
- ๐
๐ฎ๐ง๐๐ญ๐ข๐จ๐ง๐๐ฅ ๐๐จ๐ฆ๐ฉ๐จ๐ง๐๐ง๐ญ๐ฌ: These are simple functions that return JSX.
- ๐๐ฅ๐๐ฌ๐ฌ ๐๐จ๐ฆ๐ฉ๐จ๐ง๐๐ง๐ญ๐ฌ: These are ES6 classes that extend
React.Component
and include additional features like state and lifecycle methods.
Components make your UI modular and reusable, which is one of the biggest strengths of React.
2 Likes
๐๐๐ฒ ๐๐ of #100DaysOfCode
- ๐น๐๐๐๐ ๐บ๐๐๐๐ ๐๐๐
๐ท๐๐๐๐ -
State and props are core concepts in React that allow you to manage and pass data within your application. While props are immutable and passed from parent to child components, state is mutable and managed within the component itself.
-
๐๐ญ๐๐ญ๐:
State is a JavaScript object that holds dynamic data that can change over time. It is typically managed within class components or functional components using hooks.
-
๐๐ซ๐จ๐ฉ๐ฌ:
Props (short for properties) are used to pass data from one component to another. They are immutable, meaning that they cannot be modified by the receiving component.
2 Likes
๐๐๐ฒ ๐๐ of #100DaysOfCode
- ๐น๐๐๐๐ ๐ฏ๐๐๐๐ -
React Hooks are functions that let you use state and other React features in functional components. Introduced in React 16.8, hooks provide a way to manage state and lifecycle methods without needing to write class components.
๐๐ผ๐บ๐บ๐ผ๐ป ๐๐ผ๐ผ๐ธ๐:
- ๐ฎ๐ฌ๐๐๐ญ๐๐ญ๐: Allows you to add state to functional components.
- ๐ฎ๐ฌ๐๐๐๐๐๐๐ญ: Lets you perform side effects in your components, such as fetching data or subscribing to events.
- ๐ฎ๐ฌ๐๐๐จ๐ง๐ญ๐๐ฑ๐ญ: Provides a way to share values like props and state across components without passing them explicitly through every level.
2 Likes
๐๐๐ฒ ๐๐ of #100DaysOfCode
- ๐น๐๐๐๐ ๐ณ๐๐๐๐๐๐๐๐ ๐ด๐๐๐๐๐
๐: ๐ด๐๐๐๐๐๐๐ ๐ช๐๐๐๐๐๐๐๐ ๐ณ๐๐๐๐๐๐๐๐ -
Lifecycle methods are functions that get invoked at different stages of a componentโs existence in a React application. These methods allow you to perform actions like fetching data, cleaning up resources, or handling updates.
๐๐ผ๐บ๐บ๐ผ๐ป ๐๐ถ๐ณ๐ฒ๐ฐ๐๐ฐ๐น๐ฒ ๐ ๐ฒ๐๐ต๐ผ๐ฑ๐:
โ ๐๐จ๐ฆ๐ฉ๐จ๐ง๐๐ง๐ญ๐๐ข๐๐๐จ๐ฎ๐ง๐ญ: Invoked after the component is mounted to the DOM. Useful for data fetching.
โ ๐๐จ๐ฆ๐ฉ๐จ๐ง๐๐ง๐ญ๐๐ข๐๐๐ฉ๐๐๐ญ๐: Called when the component updates due to changes in props or state.
โ ๐๐จ๐ฆ๐ฉ๐จ๐ง๐๐ง๐ญ๐๐ข๐ฅ๐ฅ๐๐ง๐ฆ๐จ๐ฎ๐ง๐ญ: Invoked just before the component is removed from the DOM. Useful for cleanup tasks.
2 Likes
๐๐๐ฒ ๐๐ of #100DaysOfCode
- ๐ฏ๐๐๐
๐๐๐๐ ๐ญ๐๐๐๐ ๐๐ ๐น๐๐๐๐ -
Forms are a common feature in web applications, and React provides a way to handle form elements and user input in a more controlled and efficient manner. React forms can be either controlled or uncontrolled.
โ ๐๐จ๐ง๐ญ๐ซ๐จ๐ฅ๐ฅ๐๐ ๐๐จ๐ฆ๐ฉ๐จ๐ง๐๐ง๐ญ๐ฌ:
In controlled components, form data is handled by the React component. The component controls the input via the state.
โ ๐๐ง๐๐จ๐ง๐ญ๐ซ๐จ๐ฅ๐ฅ๐๐ ๐๐จ๐ฆ๐ฉ๐จ๐ง๐๐ง๐ญ๐ฌ:
In uncontrolled components, form data is handled by the DOM itself, and React accesses the data using refs.
1 Like
๐๐๐ฒ ๐๐ of #100DaysOfCode
- ๐น๐๐๐๐ ๐น๐๐๐๐๐: ๐ต๐๐๐๐๐๐๐๐๐ ๐ฉ๐๐๐๐๐๐ ๐ท๐๐๐๐ -
React Router is a popular library for managing navigation and routing in React applications. It allows you to create single-page applications with multiple views, enabling users to navigate between pages without a full page reload.
The attached example shows how to set up basic routes using React Router, including navigation links and route components.
2 Likes
๐๐๐ฒ ๐๐ of #100DaysOfCode
- ๐ท๐๐๐๐๐๐๐๐๐๐ ๐ถ๐๐๐๐๐๐๐๐๐๐๐ ๐๐ ๐น๐๐๐๐ -
Performance is critical in web applications, and React provides several techniques to optimize your applicationโs performance.
๐๐ฒ๐ ๐ง๐ฒ๐ฐ๐ต๐ป๐ถ๐พ๐๐ฒ๐:
โ ๐๐๐ฆ๐จ๐ข๐ณ๐๐ญ๐ข๐จ๐ง: Use ๐๐ฆ๐ข๐ค๐ต.๐ฎ๐ฆ๐ฎ๐ฐ
to prevent unnecessary re-renders.
โ ๐ฎ๐ฌ๐๐๐๐ฅ๐ฅ๐๐๐๐ค ๐๐ง๐ ๐ฎ๐ฌ๐๐๐๐ฆ๐จ ๐๐จ๐จ๐ค๐ฌ: Optimize functions and calculations that donโt need to run on every render.
โ ๐๐๐ณ๐ฒ ๐๐จ๐๐๐ข๐ง๐ : Load components and routes lazily using ๐๐ฆ๐ข๐ค๐ต.๐ญ๐ข๐ป๐บ
and ๐๐ถ๐ด๐ฑ๐ฆ๐ฏ๐ด๐ฆ
.
โ ๐๐จ๐๐ ๐๐ฉ๐ฅ๐ข๐ญ๐ญ๐ข๐ง๐ : Split your code into smaller bundles to improve load time.
2 Likes
๐๐๐ฒ ๐๐ of #100DaysOfCode
- ๐ผ๐๐
๐๐๐๐๐๐๐
๐๐๐
๐๐๐๐บ๐๐๐๐
๐๐ ๐น๐๐๐๐ -
The ๐ช๐จ๐๐๐ฉ๐๐ฉ๐
hook is one of the most fundamental hooks in React, allowing you to add state to your functional components. Before hooks, state was only available in class components, but with the introduction of hooks, you can now manage state directly in functional components, making them more powerful and versatile.
๐๐ผ๐ ๐๐๐ฒ๐ฆ๐๐ฎ๐๐ฒ
๐ช๐ผ๐ฟ๐ธ๐
When you call useState
, it returns an array with two elements: the current state and a function to update that state. You can then use array destructuring to capture these values.
The attached example demonstrates how useState
allows you to manage and update state within a functional component.
๐๐ฒ๐ ๐ฃ๐ผ๐ถ๐ป๐๐:
useState
can store any data type (numbers, strings, arrays, objects).
- The state update via
setState
re-renders the component.
- You can initialize the state with a value or a function that returns a value.
๐๐จ๐ง๐๐ฅ๐ฎ๐ฌ๐ข๐จ๐ง: The useState
hook is your go-to for managing state in functional components. Itโs easy to use and makes your components more dynamic and interactive.
2 Likes
๐๐๐ฒ ๐๐ of #100DaysOfCode
๐๐๐๐ฌ๐๐๐๐๐
๐๐๐ ๐บ๐๐
๐ ๐ฌ๐๐๐๐๐๐ -
The ๐ช๐จ๐๐๐๐๐๐๐ฉ
hook is crucial for handling side effects in your React components. Side effects include tasks like data fetching, updating the DOM, or setting up subscriptions. ๐ช๐จ๐๐๐๐๐๐๐ฉ
allows you to perform these actions while keeping your components functional and clean.
๐๐ผ๐ ๐ช๐จ๐๐๐๐๐๐๐ฉ
๐ช๐ผ๐ฟ๐ธ๐
๐ถ๐ด๐ฆ๐๐ง๐ง๐ฆ๐ค๐ต
runs after every render by default. You can control when it runs by passing a second argumentโan array of dependencies. It will only re-run if one of the dependencies changes.
๐๐ฒ๐ ๐ฃ๐ผ๐ถ๐ป๐๐:
๐ถ๐ด๐ฆ๐๐ง๐ง๐ฆ๐ค๐ต
runs after every render unless dependencies are provided.
- Itโs ideal for data fetching, subscriptions, or manual DOM updates.
- Cleanup functions are important for avoiding resource leaks.
๐๐จ๐ง๐๐ฅ๐ฎ๐ฌ๐ข๐จ๐ง: ๐ช๐จ๐๐๐๐๐๐๐ฉ
is powerful for managing side effects in functional components, ensuring that your application behaves as expected across renders.
2 Likes
๐๐๐ฒ ๐๐ of #100DaysOfCode
- ๐บ๐๐๐๐๐๐๐๐๐๐ ๐บ๐๐๐๐ ๐ด๐๐๐๐๐๐๐๐๐ ๐๐๐๐
๐๐๐๐ช๐๐๐๐๐๐
-
The ๐๐๐๐ช๐๐๐๐๐๐
hook provides a way to share values between components without passing props down manually at every level. Itโs part of the React Context API, which enables global state management in your application.
๐๐ผ๐ ๐๐๐๐ช๐๐๐๐๐๐
๐ช๐ผ๐ฟ๐ธ๐
๐๐๐๐ช๐๐๐๐๐๐
accepts a context object and returns the current context value for that context. The context value is determined by the nearest <Context.Provider>
above the calling component in the tree.
๐๐
๐ฝ๐น๐ฎ๐ป๐ฎ๐๐ถ๐ผ๐ป ๐ณ๐ผ๐ฟ ๐๐ต๐ฒ ๐๐๐๐ฎ๐ฐ๐ต๐ฒ๐ฑ ๐๐ผ๐ฑ๐ฒ:
createContext('light')
creates a context with โlightโ as the default value.
useContext(ThemeContext)
retrieves the current value of ThemeContext
.
- The theme is provided via
<ThemeContext.Provider>
and changes based on the button click.
๐๐ฒ๐ ๐ฃ๐ผ๐ถ๐ป๐๐:
๐๐๐๐ช๐๐๐๐๐๐
simplifies state management across component trees.
- It works well for theming, user authentication, and global settings.
- Combine it with
useReducer
for more complex state logic.
๐๐จ๐ง๐๐ฅ๐ฎ๐ฌ๐ข๐จ๐ง: ๐๐๐๐ช๐๐๐๐๐๐
is an essential tool for managing global state in your React application, reducing the need for prop drilling and making your code more maintainable.
2 Likes
๐๐๐ฒ ๐๐ of #100DaysOfCode
- ๐บ๐๐๐๐ ๐ด๐๐๐๐๐๐๐๐๐ ๐๐๐๐
๐๐๐๐น๐๐
๐๐๐๐
๐๐ ๐น๐๐๐๐ -
When building React applications, managing state can become complex, especially as your app grows. While useState
is great for simple state management, ๐๐๐๐น๐๐
๐๐๐๐
shines in scenarios where state logic is more intricate.
๐ช๐ต๐ฎ๐ ๐ถ๐ ๐๐๐๐น๐๐
๐๐๐๐
?
๐๐๐๐น๐๐
๐๐๐๐
is a hook thatโs ideal for managing complex state logic in React. Itโs inspired by the Redux pattern but doesnโt require setting up a full Redux store. Instead, ๐๐๐๐น๐๐
๐๐๐๐
provides a way to handle state transitions through actions and a reducer function.
๐๐ผ๐ ๐ฑ๐ผ๐ฒ๐ ๐๐๐๐น๐๐
๐๐๐๐
๐๐ผ๐ฟ๐ธ?
๐๐๐๐น๐๐
๐๐๐๐
takes two arguments: a reducer function and an initial state. It returns a state variable and a dispatch function that you can use to update the state.
๐๐
๐ฝ๐น๐ฎ๐ป๐ฎ๐๐ถ๐ผ๐ป ๐ผ๐ณ ๐๐๐๐ฎ๐ฐ๐ต๐ฒ๐ฑ ๐๐ผ๐ฑ๐ฒ:
- ๐๐๐๐ฎ๐๐๐ซ ๐๐ฎ๐ง๐๐ญ๐ข๐จ๐ง: This function takes the current state and an action, then returns a new state based on the action type.
- ๐๐ง๐ข๐ญ๐ข๐๐ฅ ๐ฌ๐ญ๐๐ญ๐: The starting value of your state, defined outside the component.
- ๐๐ข๐ฌ๐ฉ๐๐ญ๐๐ก ๐๐ฎ๐ง๐๐ญ๐ข๐จ๐ง: Used to send an action to the reducer, triggering a state change.
๐ช๐ต๐ฒ๐ป ๐๐ผ ๐๐๐ฒ ๐๐๐๐น๐๐
๐๐๐๐
?
โ ๐๐จ๐ฆ๐ฉ๐ฅ๐๐ฑ ๐ฌ๐ญ๐๐ญ๐ ๐ฅ๐จ๐ ๐ข๐: When state transitions depend on multiple conditions or actions.
โ ๐๐๐ฅ๐๐ญ๐๐ ๐ฌ๐ญ๐๐ญ๐ ๐ฏ๐๐ซ๐ข๐๐๐ฅ๐๐ฌ: When multiple state variables need to be updated simultaneously.
โ ๐๐ซ๐๐๐ข๐๐ญ๐๐๐ฅ๐ ๐ฌ๐ญ๐๐ญ๐ ๐ฎ๐ฉ๐๐๐ญ๐๐ฌ: Helps to ensure state transitions are predictable and easier to test.
๐๐๐๐น๐๐
๐๐๐๐
is particularly useful in situations where managing state with useState
would lead to cluttered or less maintainable code.
2 Likes