ComponentDidUpdate firing multiple time instead of once per update

My component is performing calculation based on the result from before user performs a change, and afterwards, and for some reason, the component seems to be running the function multiple times instead of once per change, which is resulting in inaccurate results. Any suggestion on what could be causing the problem?

This is the code:

import * as React from 'react';
import { ILiquidiManagerProps } from './ILiquidiManagerProps';
import {AccountValue} from './AccountFields';
import {LiquidTEUR} from './DummyContent';

interface SubtotalState {
    Account?: any,
    SubtotalValue?: any,
    iteration?: number,
  }

export default class SubtotalValues extends React.Component<ILiquidiManagerProps, SubtotalState> {
    public constructor(props: ILiquidiManagerProps) {
        super(props);
        this.state = {
            Account: LiquidTEUR.Subcategories.find(item => item.Name == this.props.label),
            SubtotalValue: 0,
        }
    }
    
    private getText = data => {
        this.setState({SubtotalValue: data});
    }

    private initiateValue = () => {
        let Account = this.state.Account;
        let subtotal = 0;
        Account.Accounts.map((item) => {
            subtotal += item.Value;
        })
        this.setState({SubtotalValue: subtotal});
    }

    componentDidMount(){
        this.initiateValue();
    }

    componentDidUpdate(_prevProps, prevState){
        if (this.state.SubtotalValue !== prevState.SubtotalValue){
            let total = this.props.liquidTotal;
            if(this.props.label == 'Kreditlinien in TEUR'){
                total = total + prevState.SubtotalValue - this.state.SubtotalValue;
            } else {
                total = total - prevState.SubtotalValue + this.state.SubtotalValue;
            }
            this.props.getValue(total);
        }
    }

    public render(): React.ReactElement<ILiquidiManagerProps> {
        console.log(this.props.liquidTotal + "Subtotal");
        return(
            <React.Fragment>
            <a className="collapsible" href="#">
                <div 
                    className={`p-2 text-right value ` + this.props.bgColor + ` font-weight-bold ` + this.props.textColor}
                    data-date={this.props.date}
                    data-name={this.props.label}
                >{this.state.SubtotalValue.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".")}</div></a>
            <div className="content hidden">
                {this.state.Account.Accounts.map((item, key) => {
                    return <AccountValue key={key} label={item.Value} getValue={this.getText} subtotal={this.state.SubtotalValue} />;
                })}
                <span className="add-pos">&nbsp;</span>
            </div>
            </React.Fragment>
        );
    }
}

161 thoughts on “ComponentDidUpdate firing multiple time instead of once per update”

  1. Pingback: sildenafil 20mg
  2. Pingback: cost of viagra
  3. Pingback: cialis capsule
  4. Pingback: herbal viagra
  5. Pingback: cheap cialis
  6. Pingback: viagra otc
  7. Pingback: viagra buy online
  8. Pingback: buy cialis viagra
  9. Pingback: tadalafil dosages
  10. Pingback: sildenafil 50mg
  11. Pingback: chewable viagra
  12. Pingback: viagra ohne rezept
  13. Pingback: viagra homme
  14. Pingback: buying viagra
  15. Pingback: viagra gum
  16. Pingback: viagra cock
  17. Pingback: viagra users group
  18. Pingback: sildenafil citrate
  19. Pingback: cheap viagra
  20. Pingback: viagra prices
  21. Pingback: hims viagra
  22. Pingback: viagra price
  23. Pingback: viagra online
  24. Pingback: buy viagra

Leave a Comment