Skip to main content

Mastering Parent-Child Data Sharing in Angular and Next.js

Passing Data from Parent to Child

Angular Approach

In Angular, to pass data from a parent component to a child component, we can use the @Input() decorator. First, let's create a child component called "Child" and import it into the main "App" component.

In the child component's TypeScript file, we can define an @Input() property to hold the data from the parent:


@Input() content: string;

Now, in the parent "App" component, we can pass the data to the child component using the property name defined in the child component:



Where "contentFromParent" is a string value defined in the parent component's TypeScript file.

Instead of a simple string, we can also pass an array of objects or a list of values to the child component. In this case, we'll use the property name "states" to hold the data:



In the child component, we can then loop through the "states" array and display the data:


  •  
  • {{ state.name }}
  •  

Next.js Approach

In Next.js, the process of passing data from a parent component to a child component is similar. Let's create a child component called "Child.jsx" and use it in the parent page.

In the parent page, we can define the data we want to pass to the child component and then pass it as a prop:


const [states, setStates] = useState(['California', 'New Jersey']);

return (
  
);

In the child component, we can then access the "states" prop and render the data:


const Child = ({ states }) => {
  return (
    
  • {states.map((state, index) => (
  • {state}
  • ))}

  );
};

Passing Data from Child to Parent

Angular Approach

To pass data from a child component to a parent component in Angular, we can use the @Output() decorator and an EventEmitter. In the child component, we'll define an @Output() property and an event emitter:


@Output() sendData = new EventEmitter();

sendToParent() {
  this.sendData.emit('Message from child to parent');
}

In the parent component, we can listen for the "sendData" event and handle the data received from the child:




handleDataFromChild(message: string) {
  this.messageFromChild = message;
}

Next.js Approach

In Next.js, we can use the same concept of passing data from a child component to a parent component. In the child component, we'll define a function to send the data to the parent:


const sendDataToParent = (event) => {
  event.preventDefault();
  onChangeHandler('Message from child to parent');
};

In the parent page, we can define the "onChangeHandler" function and pass it as a prop to the child component:


const [childData, setChildData] = useState(null);

const onChangeHandler = (data) => {
  setChildData(data);
};

return (
  
  {childData && 

Data from child: {childData}

}
);

By following these approaches, you can effectively share data between parent and child components in both Angular and Next.js applications, making your application more dynamic and interactive.

 

Comments

Popular posts from this blog

Ultimate Guide to Implementing DELETE Operations in Next.js with Prisma

In this tutorial, we will learn how to delete a post in Next.js using the API and UI code. Deleting a post requires making changes to the folder structure and implementing the delete method. Let's dive in! Changing the Folder Structure Currently, the folder structure for the API is not suitable for deleting posts. We need to pass an ID to delete a specific post. To enable this functionality, we will need to alter the folder structure. Here's how: Create a new folder named "posts" within the API directory. Move the existing file into the "posts" folder. Rename the file to "index". Now, when we access the "posts" endpoint, it will work without specifying the file name. Implementing the Delete Method To delete a post, we need to create a new file in the "posts" folder and pass the I...

Creating API Routes for a Full Stack Application

In this blog post, we will explore how to create API routes for a full stack application. API routes are essential for connecting the frontend components to the backend database and fetching data. We will specifically focus on creating the API routes and endpoints for making API calls and retrieving data. Setting up the Database Before we dive into creating the API routes, we need to set up the database. In this example, we will be using a local PostgreSQL database. To connect to the database, we have a schema file that defines a table called "todos" with four fields: id, name, username, and created_at. Currently, we have two records in the todos table. The next step is to write the API routes to connect to the database and fetch the data. Using Prisma for API Routing To create the API routes, we will be using the Prisma library, specifically the Prisma client. The Prisma client allows us to connect to the database and execute queries. To start with the API rout...

How to use Redux in Next.js

Introduction In this blog post, we will explore how to use Redux with Next.js and create todo and disptach it to Redux store and and pass the todo (state) to a Page and display the created Todo. We will cover the process of setting up the necessary Redux store and cover the end to end flow. Create Next.js application npx create-next-app@latest Install dependencies npm install --save @reduxjs/toolkit npm install --save react-redux npm install --save redux-persist Folders and files needed Create folder lib under src folder(src/lib) Create Interface Todos under lib folder (lib/Todos.ts) export interface ITodo { todosState: [Todo] } export interface Todo { todoName: string, description: string, userName: string, id: string } Create folder store under lib (lib/store) Create fi...