The Journey of #100DaysOfCode (@Ayman_Dandan)

๐ƒ๐š๐ฒ ๐Ÿ’๐Ÿ 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!

  1. $๐ฌ๐ž๐ญ
    The $๐ฌ๐ž๐ญ operator is used to update the value of a field in a document. If the field does not exist, it will be added.

  2. $๐ข๐ง๐œ
    The $๐ข๐ง๐œ operator increments the value of a field by a specified amount. Itโ€™s useful for counters, stock management, or any numeric updates.

  3. $๐ฉ๐ฎ๐ฌ๐ก
    The $๐ฉ๐ฎ๐ฌ๐ก operator adds an element to an array field. If the array does not exist, it will be created.

  4. $๐ ๐ญ๐ž 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.

  5. $๐จ๐ซ
    The $๐จ๐ซ operator is used to perform logical OR operations on an array of conditions. It returns documents that match any of the given conditions.

  6. $๐ข๐ง
    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.
1724180271199

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.

๐—ง๐˜†๐—ฝ๐—ฒ๐˜€ ๐—ผ๐—ณ ๐— ๐—ถ๐—ฑ๐—ฑ๐—น๐—ฒ๐˜„๐—ฎ๐—ฟ๐—ฒ:

  1. ๐€๐ฉ๐ฉ๐ฅ๐ข๐œ๐š๐ญ๐ข๐จ๐ง-๐‹๐ž๐ฏ๐ž๐ฅ ๐Œ๐ข๐๐๐ฅ๐ž๐ฐ๐š๐ซ๐ž:
  • Bound to the Express app using app.use() or app.METHOD().
  • Can apply to all routes or specific ones.
  1. ๐‘๐จ๐ฎ๐ญ๐ž๐ซ-๐‹๐ž๐ฏ๐ž๐ฅ ๐Œ๐ข๐๐๐ฅ๐ž๐ฐ๐š๐ซ๐ž:
  • Works similarly to application-level middleware but is bound to an instance of express.Router().
  1. ๐„๐ซ๐ซ๐จ๐ซ-๐‡๐š๐ง๐๐ฅ๐ข๐ง๐  ๐Œ๐ข๐๐๐ฅ๐ž๐ฐ๐š๐ซ๐ž:
  • Specifically designed to handle errors in your application.
  1. ๐๐ฎ๐ข๐ฅ๐ญ-๐ข๐ง ๐Œ๐ข๐๐๐ฅ๐ž๐ฐ๐š๐ซ๐ž:
  • Express.js provides built-in middleware for common tasks like serving static files (express.static), parsing JSON (express.json), and more.
  1. ๐“๐ก๐ข๐ซ๐-๐๐š๐ซ๐ญ๐ฒ ๐Œ๐ข๐๐๐ฅ๐ž๐ฐ๐š๐ซ๐ž:
  • 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.
1724429022961
1724429022928

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.
1724430922792
1724430923366

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.

    1724710810667
4 Likes

๐ƒ๐š๐ฒ ๐Ÿ“๐ŸŽ of #100DaysOfCode :tada:

  • ๐‘น๐’†๐’‚๐’„๐’• ๐‘ช๐’๐’Ž๐’‘๐’๐’๐’†๐’๐’•๐’” -

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.
    1724768243852
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.
1724768602186

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