Posts

Showing posts from 2024

Function Components vs Class Components in React – With Examples

In React, there are two primary ways to create components: function and class components. Each has its own syntax and use cases, although with the introduction of React Hooks, the gap between them has narrowed significantly. But the selection of appropriate component types is still very crucial for building efficient and maintainable React applications. In this article, we'll explore the foundational differences between function and class components, providing a clear understanding of their strengths and ideal use cases. By understanding these concepts, developers can make informed decisions when constructing React components, ultimately enhancing the structure and functionality of their web applications. What are React Components? In React, components are the building blocks of a user interface. They are reusable, self-contained pieces of code that represent a part of the UI. React allows you to break down your UI into smaller components, which makes it easier to manage and mainta...

Migrations with TypeORM in NestJs

Migrations with TypeORM in NestJs Why Do We Need Migrations in NestJs? NestJs provides an auto-sync feature that automatically updates the database when entity changes occur. Given this, you might wonder why migrations are necessary. The reason is that while auto-sync can be convenient, it can become risky in production environments. When your application is live and contains actual data, direct modifications to the database could lead to issues. This is why relying on synchronization in production is considered unsafe. Advantages of Using Migrations You can control when migrations are applied. Reduces the risk of errors in production environments. Migration files can be automatically generated from entity changes, so you don’t need to manually write them. Disadvantages of Migrations You need to generate and run migration files every time you make changes to your entities. Example Suppose you have an entity named Student : import { BaseEntity , Column , Entity , PrimaryGeneratedColumn...

Circular Dependencies in NestJS and How to Prevent Them

So, you encounter a scenario where you're developing your NestJS server application smoothly, only to be interrupted by an error message like this: [ Nest ] 116557 - 10 /07/2022, 2 :12:40 PM ERROR [ ExceptionHandler ] Nest cannot create the BarModule instance. The module at index [ 0 ] of the BarModule "imports" array is undefined. Potential causes: - A circular dependency between modules. Use forwardRef ( ) to avoid it. Read more: https://docs.nestjs.com/fundamentals/circular-dependency - The module at index [ 0 ] is of type "undefined" . Check your import statements and the type of the module. Scope [ AppModule - > FooModule ] You pause, analyze the situation, and realize that adding a forwardRef to your  BarModule  and  FooModule  imports might resolve the issue, correct? Nest can't resolve dependencies of the FooService ( ? ) . Please make sure that the argument dependency at index [ 0 ] is available in the FooModule context. Potenti...