Angular 2's OnPush Strategy fails to detect changes in Array

Within my application, I have two components - a parent component and a child component. The child component has an OnPush Strategy implemented to detect changes efficiently. Through binding using (), I pass an array of objects from the parent to the child component.

However, after everything is rendered and I update a field of one of the objects in the array, the template that lists the fields values does not automatically update. Can anyone provide insight into why this might be happening?

Answer №1

OnPush requires immutability to work effectively.

If you modify your array using the push method, your component will not be updated because the reference to the array remains the same.

In the parent component, when adding a value, it is recommended to use:

this.myArray = [...this.myArray, {someObjectOrWhatever: ''}] // this creates a new reference

For removal, consider using filter which also returns a new array.

As long as you maintain the same reference and have OnPush enabled in your child component, updates will not be visible.

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

Instead of adding an object to the array, the _id is simply pushed in

In my current setup, I am using nodejs with typescript along with mongoose and express. After running a mongoose query, I receive a league document that has multiple levels of data. One specific requirement is to reorder the populated array ties based on c ...

Navigating json data in angular 6

I retrieved a JSON object in the following format response = [ { 'a': [ { 'b': [ { 'c': [ { 'name': 'abc', 'value': 900 ...

What is the significance of using an underscore before a function name?

I'm curious about the use of an underscore before a function. For example, what does it signify in _someFunction(){...}? I often use underscores for partial files in SCSS, such as _buttons.scss or _layout.scss. However, I see this practice commonly i ...

Display fresh information that has been fetched via an HTTP request in Angular

Recently, I encountered an issue where data from a nested array in a data response was not displaying properly in my component's view. Despite successfully pushing the data into the object programmatically and confirming that the for loop added the it ...

The incorrect argument is being used to infer the generic type

I have implemented the NoInfer feature from the library called ts-toolbelt. However, it seems that the example provided below does not reflect its intended effect. In this scenario, the generic type is deduced from the util function as soon as it is intr ...

Webpack and TypeScript error: The configuration object is invalid due to 'configuration.resolve.extensions[0]'

I recently completed a tutorial on webpack without typescript, and then dove into another one on configuring webpack with typescript from this link. However, when I tried to build the project using npm run build as instructed in the second tutorial, I enco ...

Is there a method to instruct angular-cli (for angular 2) to produce a minified version of css files?

When running "ng serve" in angular-cli, the generated css is not minified as expected. Is there a specific setting to configure in angular-cli-build or an additional plugin to install? This is the content of my angular-cli-build.js file: var Angular2App ...

Struggling with setting up Role-Based Access Control (RBAC) with cookie authentication in React

I've been working on incorporating Role Based Access Control into a React app using cookies, but I'm struggling to understand its use. The idea was to create a context that retrieves the data stored in the cookie through a specific API endpoint d ...

What is the process for mocking a method from a class that is instantiated within another class using ts mockito in typescript?

I have a specific Class that I want to test using the mocha-chai testing framework in TypeScript. My approach involves incorporating ts-mockito for mocking purposes. export class MainClass implements IMainClass { private mainResource: IMainResource; ...

Sharing data between two components without using inheritance in Angular 2

I've been attempting to transfer a single variable from one component to another. The variable is an array and is initialized as shown below. @Component({ selector: 'component1', templateUrl: './component1.component.html', s ...

Guide to verifying the existence of a specific object mapping in JavaScript

I am currently working with an object mapping called res.payload.data[0].tnxResponse. I need to verify that the res object contains a payload property which in turn has a data property and so on. I attempted to do this using the following code, but it resu ...

Adding a new row with a maximum number of columns in the material table

My table creation process involves adding an extra row with a progress bar after each existing row, similar to this: https://i.sstatic.net/yVsmp.png The second tr in my setup has a colspan="8" I want to achieve the same layout as the material ...

What could be causing this error when running nodemon in an Angular TS file?

One issue I am facing is that upon running the npm run dev command, I encounter the error message stating that 'node_modules is not recognized as an internal or external command'. Here is an image showing the specific error: https://i.sstatic.ne ...

Exploring ways to migrate the component functions to the service in Angular

I am looking to move the functionality of the up function to a service and then call it in the component. A basic app that is structured as follows: The service contains 2 functions: getRandomNumbers - Displays a random number between 1 and 20 every ...

Modify data in a table using Dialog Component in Angular Material

I need to implement a Material Dialog feature that allows users to update entries in a table by clicking on the "Change Status" button. Check out this functional snippet: https://stackblitz.com/edit/angular-alu8pa I have successfully retrieved data fr ...

Animating Chart.js inside an Angular mat-tab component

I have a situation where I'm displaying multiple charts within a mat-tab, but I'm experiencing an issue with the animation of data in the chart. animation: { duration: 1000, easing: 'easeOutQuart' } The a ...

Encountering net::ERR_FILE_NOT_FOUND error when trying to upload chunked files through Ionic4 and Angular HttpClient, even after several successful uploads

I am currently facing an issue while attempting to upload a large file (500+Mb, potentially larger) to our php server using an app developed with Ionic4, Angular, and Cordova on an Android 10 emulator. To tackle this, I have implemented a mechanism to uplo ...

What is the best way to implement this object builder in TypeScript strict mode without adding unnecessary weight to it?

After coming across a builder pattern online that I really liked, I found that it doesn't work in strict mode due to receiving the same error for the first 3 properties: (property) PizzaBuilder.numberOfSlices: number Property 'numberOfSlices&apo ...

What is the best method for conducting comprehensive testing of all projects and libraries within NestJS (nx)?

Our NestJS project has been established with multiple libraries through Nx. We have successfully run tests on individual projects/libraries using the following command: npx nx test lib1 --coverage While this method works well, we are faced with numerous l ...

Obtaining the Final Document Identifier for Paginating with valueChanges()

When using valueChanges() in AngularFire to subscribe to Firestore data, I encountered an issue where obtaining the last document reference for pagination was not as straightforward as when using onSnapshot. Unfortunately, the approach I was using with o ...