The Angular NGRX action payload seems to undergo modifications between dispatching and execution

Angular v10, NGRX v10

I am facing a perplexing issue with my Plan object and Task properties using Angular v10 and NGRX v10. Despite making updates to Task properties within my TaskService by deep cloning the Plan object and dispatching an Action to update the Store, I notice that the Task properties revert back to their original values when logging in the Reducer. This has left me puzzled for four days now.

The process begins with dragging a Task on a Gantt Chart triggering the

TaskService.updateTasks(tasksToUpdate)
method, which is supposed to handle any changes made during user interaction.

To troubleshoot, I created a simplified version of my updateTasks method called updateTasksSimple, but the problem persists. Here is the simplified method:

 // Simplified updateTasks method
 public updateTasksSimple(tasks: ITask[]): void {
    // Code here
 }

Further investigation revealed that even after logging the updated Tasks before dispatching from TaskService, the Redux store doesn't reflect these updated values when accessed by other components subscribed to the Store. This inconsistency has me baffled.

Any guidance or insights on this matter would be greatly appreciated.

Update:

Upon further examination, I noticed a peculiar behavior in my app with regards to updating Task properties. Despite successfully updating Task properties on the Gantt page and dispatching actions to the Store, the Task page does not display the updated values if navigated directly. However, if I reload the app, update the Tasks twice on the Gantt page, and then navigate to the Task page, it shows the correct values. It seems like the initial values received by the observable are outdated, leading to this discrepancy.

This strange behavior has left me scratching my head as to why the initial values received by the observable do not reflect the current state in the Store. Any suggestions or assistance on resolving this issue would be highly valued.

Answer №1

One thing that raises suspicion is the way you handle the assignment of tasks in the code snippet. Instead of simply assigning tasks by reference, consider creating a deep copy of the updated tasks:

    // Map updated tasks
    updatedPlan.tasks = tasksToUpdate.map(t => ({
      ...t,
      t.start = new Date(2021, 2, 26, 16);
      t.end = new Date(2021, 2, 26, 22);
      t.durationHours = 6;
    }));
    // It might be better to avoid this line
    // updatedPlan.tasks = tasksToUpdate;

Answer №2

Through thorough investigation, I discovered that the issue stemmed from my misuse of NGRX. In a different section of my code, I had subscribed to a selector that returned currentPlanWithChildren.tasks, an Observable<ITask[]>. From there, I inadvertently mutated the Tasks object while sorting the array, which ultimately caused the problem. To resolve this, I simply adjusted my sorting method to tasks.slice().sort(t => ...

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

What is the correct way to write SVG markup within SVG tags in a React and NextJS environment?

I currently have a Svg component set up like this interface SvgIconProps { children: React.ReactNode; strokeWidth?: number; width?: number; height?: number; className?: string; } export const SvgIcon = ({ children, strokeWidth = 1, width = ...

Troubleshooting `TypeError: document.createRange is not a function` error when testing material ui popper using react-testing-library

I am currently working with a material-ui TextField that triggers the opening of a Popper when focused. My challenge now is to test this particular interaction using react-testing-library. Component: import ClickAwayListener from '@material-ui/core/ ...

Incorporate a JavaScript script into an Angular 9 application

I have been experiencing issues trying to add a script.js file to angular.json and use it in one component. Adding a script tag directly to my HTML file is not the ideal solution. Can someone suggest an alternative approach or point out what I may be missi ...

Issue - firestore has not been defined (Occurs strictly after the use of "then")

Having an issue: I successfully create fake users in my database, but encounter a problem when starting the 'for' loop. The error I'm facing is: Error adding document: TypeError: Cannot read property 'firestore' of undefined I ...

Modifying the user interface (UI) through the storage of data in a class variable has proven to be

If I need to update my UI, I can directly pass the data like this: Using HTML Template <li *ngFor="let post of posts; let i = index;"> {{i+1}}) {{post.name}} <button (click)="editCategory(post)" class="btn btn-danger btn-sm">Edit</butto ...

What steps can I take to make my animation work in the opposite direction as well?

I'm currently working with an angular slider that is set to TRUE/OPEN by default. The issue I am facing is that while I am able to slide it using angular animations in one direction, I am unable to see the transition when sliding it back. Any assistan ...

The extensive magnetic scrolling functionality in Ionic 2 sets it apart from other frameworks

Hi everyone, I could really use some assistance! I've been working on developing an Ionic 2 App and my navigation setup is not too complex. I have a main menu where clicking on an item opens another menu with a submenu. From there, if I click on an i ...

Refresh the display after adding an item to an array in Angular

Currently, I am facing an issue with updating the view based on adding an item to an array of objects through a click handler. While the item is successfully pushed into the array, it does not reflect in the view. I am wondering if placing the method withi ...

Display a message stating "No data available" using HighCharts Angular when the data series is empty

My Angular app utilizes Highchart for data visualization. One of the requirements is to display a message within the Highchart if the API returns an empty data set. I attempted a solution, but unfortunately, the message does not appear in the Highchart a ...

Building a dynamic form in Angular with nested JSON data for enhanced reactivity

Although similar questions have been asked before, I am struggling with a large nested JSON value like the following: "Q-1": { "question": { "text": "What are your hopes and goals for this yea ...

AngularJS UI-Router in hybrid mode fails to recognize routes upon initial page load or reload

It appears that when using the @ui-router/angular-hybrid, routes registered within an ng2+ module are not being recognized during the initial load or reload. However, these same routes work fine when accessed by directly typing the URL. I have followed th ...

It appears that protractor-flake is programmed to re-run all tests instead of just the ones that have failed

Running tests using the latest version of "[email protected]", encountering failures during testing but the tests are rerunning again. No custom reporter used except Allure reporting. Below is the command used for running: protractor-flake --max-at ...

The Auth0 callback function is functioning properly in the development environment of my Angular 2 (4) application

While testing my Angular 4 login with Auth0 in development mode using localhost:4000/callback as the redirectUri for my auth0.WebAuth, everything works smoothly. However, when transitioning to production and changing the redirectUri = https://example.com/ ...

How to invoke a method in a nested Angular 2 component

Previous solutions to my issue are outdated. I have a header component import { Component, OnInit } from '@angular/core'; import { Router, NavigationEnd } from '@angular/router'; import { Observable } from 'rxjs/Observable'; ...

Ways to retrieve the component name from a service in Angular without relying on private APIs such as view container refs

How can I access the component name inside a service that is calling a method within the service? I have tried using console.log(this.vcr['_view'].component) and console.log(this.vcr['_view'].component.constructor.name), but they do not ...

Receiving an error message "Cannot read property 'X' of undefined" when invoking a sibling method

I'm completely new to Angular and trying to understand how events work within this framework. Here is my Parent Template setup: <child1 (myEvent)="child2.testMethod()"></child1> <child2 #child2 *ngIf="show"></child2> When I ...

Tips on showcasing multiple values using Angular 4?

Using angular 4 and spring boot, I retrieve a list of records from spring boot to angular 4 but only display a single object in the form. How can I list multiple values in the search box? component.ts: Within this component, all the values are retrieved b ...

Using Angular 4 to pass a specific array element from the parent component to the child component and showcasing its value in the child's HTML

I am currently working on an Angular 4 application and in need of passing an array called NpvResults from the parent component to a child component. The goal is to access this array within the child component and display its value to the client of the chil ...

Arrange the elements within the anchor tag in a vertical line using Angular Material

Is there a way to style the elements within an anchor tag in Angular Material so that they display in a single line? I am looking for the CSS solution to achieve this outcome. This is my current HTML code: <a mat-list-item [routerLink]="['das ...

Vue3 and Typescript issue: The property '$el' is not recognized on type 'void'. What is the correct way to access every existing tag type in the DOM?

Currently, I am in the process of migrating a Vue2 project to Vue3 and Typescript. While encountering several unusual errors, one particular issue with $el has me puzzled. I have been attempting to target every <g> tag within the provided template, ...