Creating complex hierarchical structures [Using Angular and Ngrx]

Question I have a complex nested array structure with multiple layers of children. The API response is flat, but I can determine the parent based on ParentId.

Within my HTML, I need to iterate through each object and display its children. Is there an efficient way to write a createSelector and select a state at each iteration in the HTML, so that updating a nested child only re-renders the specific object that was changed?

I've researched Angular material table implementation for ideas, but I'm open to hearing about other concepts or approaches.

App information:

  • Framework: Angular 5 universal
  • State management: Ngrx
  • Backend: Node + swagger

The API returns an array of objects with this structure:

{
  "id": "299999999",
  "parentId": "5ad4d",
  "body": "string",
  "type": "test"
}

Each object has a parentId field that indicates its child relationship within the hierarchy.

I store all items in a list within my application state. While this example shows 3 levels of nesting, in reality, I may have up to 20 levels deep.

interface AppState {
  list: [
{
  "id": "299999999",
  "parentId": "5ad4d",
  "body": "string",
  "type": "test"
},
{
  "id": "2abc",
  "parentId": "299999999",
  "body": "string",
  "type": "test"
},
{
  "id": "2abcd",
  "parentId": "299999999",
  "body": "string",
  "type": "test"
},
{
  "id": "2abcde",
  "parentId": "299999999",
  "body": "string",
  "type": "test"
},
{
  "id": "2abcde",
  "parentId": "6dda4",
  "body": "string",
  "type": "test"
},
{
  "id": "2abcdefg",
  "parentId": "2abcde",
  "body": "string",
  "type": "test"
},
{
  "id": "23sadasd",
  "parentId": "2abcde",
  "body": "string",
  "type": "test"
},
{
  "id": "12asdasd",
  "parentId": "2abcde",
  "body": "string",
  "type": "test"
}
];
}

Answer №1

To avoid having to re-render the entire list, you may need to consider your options carefully.

While using a memoized selector can help, it may not be sufficient on its own.

One recommended approach is to define a trackBy function within your ngFor loop. This function should take an object as a parameter and return a unique identifier for that resource, such as its ID.

By implementing this strategy, Angular will only update the necessary items in the list without having to re-render everything from scratch.

If you're interested, I've provided a detailed explanation of how to achieve this without triggering unnecessary re-renders:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Navigating through different components in Angular2 is made simple with the

Struggling to create a single-page app and facing issues with routing. Despite following several tutorials, I find them quickly becoming outdated due to Angular2 still being in beta. Whenever any reference to router directives, configurations, or provider ...

How can I enable autofocus on a matInput element when clicking in Angular 6?

Is there a way to set focus on an input element after a click event, similar to how it works on the Google Login page? I've tried using @ViewChild('id') and document.getElementId('id'), but it always returns null or undefined. How ...

"Ensure Playwright refreshes the page automatically following navigation when a specific status code is

I find myself in a dilemma where I require my functional browser tests to verify the status code of each page response, and if a 503 error is encountered, try to reload the page a certain number of times before declaring failure. Even though I have experi ...

What is the best way to display a single instance of an Angular component across multiple locations?

Is there a way to display the same angular component in multiple locations simultaneously? Specifically, I am working with the angular mat-stepper component (mat-stepper) and I require it to appear in both horizontal and vertical steppers while ensuring ...

Stop additional properties from being added to a typescript interface when converting JSON strings

Currently, I am developing an extension for Arduino on VSCode and facing an issue with a section of my code. To load the project's configuration, I am accessing a .json file located in the .vscode folder. While ideally, the user should not manually ed ...

Tips for resolving the issue shown in node_modules/@types/moment-timezone/moment-timezone.d.ts(50,73)

I encountered an issue within the node_modules directory. How should I go about resolving this ERROR in node_modules/@types/moment-timezone/moment-timezone.d.ts(50,73) Error messages: node_modules/@types/moment-timezone/moment-timezone.d.ts(50,73): erro ...

Guide to mocking the 'git-simple' branchLocal function using jest.mock

Utilizing the simple-git package, I have implemented the following function: import simpleGit from 'simple-git'; /** * The function returns the ticket Id if present in the branch name * @returns ticket Id */ export const getTicketIdFromBranch ...

During the installation process of the Angular App on a Windows operating system, the Cypress App encountered a download error while running the command "npm install

Encountering an issue when running "npm install" for Angular App on Windows while behind a corporate proxy with proper settings. The error message is as follows: npm i > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f09389 ...

Inject props into a Component nested within a Higher-Order-Component (HOC)

In attempting to grasp the concept of creating a React Higher Order Component from this particular article, I find myself struggling to fully understand and utilize this HOC. interface PopupOnHoverPropType { hoverDisplay: string; } const WithPopupOnHov ...

What are the best ways to quickly avoid changing routes in Angular?

My routing logic includes: this.route.paramMap .pipe(switchMap((parameters) => of(parameters))) .subscribe((params: Params) => { if (params.has("id")) { // Send request to server } if (params.has("block")) ...

Using ion-icon inside a clickable ion-card while applying float: right does not render properly

I am facing an issue with a list of ion-cards that have clickable icons appearing when you hover over them. The problem is that due to the floating nature of the icons, they are not positioned correctly (as shown in the image below) and end up getting cove ...

Crafting a variety of elements using Power BI's custom visual feature

I'm struggling with creating multiple objects based on input data. I've tried using an array, but it requires knowing the number of objects in advance. Does anyone have a solution for this? Here is my code snippet - module powerbi.extensibilit ...

Different Ways to Validate Angular 2 FormGroups

Component: ngOnInit() { this.record = new FormGroup({ movement: new FormControl(''), weight: new FormControl('', [Validators.required, Validators.pattern('^[0-9]*$')]), date: new FormControl('' ...

What are some effective ways to obscure my Typescript and React application?

Could you provide some guidance on obfuscating my Typescript + React application? By the way, I initiated the project using create-react-app --template typescript ...

Toggle the button's activation based on the user's input

I have set up an input field where users can select a date. I am looking to implement a simple validation that will disable the submit button if the date field is left empty, and then enable it once a date is selected. However, I am encountering an issue ...

Employ the use of expressions to retrieve and store input values within a template

<input ngModel name='quantity' placeholder='Quantity' type="number" class="form-control" pattern="^\d*(\.\d{0,2})?$" required /> <input ngModel name='price' c ...

Troubleshooting Cross-Origin Resource Sharing Problems when Accessing a Web Application from a Browser Outside an Azure Virtual Machine

Hello everyone, I currently have an Angular and Asp.net Core Application deployed on IIS within an Azure Virtual Machine. The application functions perfectly when accessed through the IIS Localhost on the Azure Virtual Machine. However, when attempting to ...

Output in Typescript for the chosen option

A message can be sent based on the user's choice of either Now or Later. If the user selects Now, the message will be sent immediately. If Later is chosen, a date format option needs to be created for setting a future date. <label for=""& ...

Ways to achieve outcomes from functions employing concatMap within rxjs?

When calling two functions, I make use of fn1 and fn2. To execute them one after the other, I utilize concatMap. I choose not to use exhaustMap and switchMap as they can result in nested "callback-hell". exhaustMap(() => fn1().pipe( swit ...

The assignment to 'total' is prohibited as it is either a constant or a property that is read-only within the get total() function

After running the command ng build --prod in my project, I encountered the following error message: "Cannot assign to 'total' because it is a constant or read-only property for function get total(){}" The function causing the issue is: get to ...