Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Trying to run my application but suffering wth "Cannot read properties of undefined (reading 'reduce')"

I'm trying to run a transactions application but Cannot read properties of undefined (reading 'reduce') error doesn't let me run the app. My screen turns into a full white btw. It was running just ok, and then suddenly boom, not working anymore: enter image description here

This is my Summary index.tsx:

import React, {useContext} from 'react'
import incomeImg from '../../assets/income.svg'
import outcomeImg from '../../assets/outcome.svg'
import totalImg from '../../assets/total.svg'
import { useTransactions } from '../../hooks/useTransactions'

import { Container } from './styles'

export function Summary() {

  const {transactions} = useTransactions()

  const summary = transactions.reduce((acc,transaction)=>{

    if (transaction.type === 'deposit'){
      acc.deposit += transaction.amount
      acc.total += transaction.amount
    } else {
        acc.withdraw = transaction.amount
        acc.total -= transaction.amount
      }

    return acc
    }, {
    deposit: 0,
    withdraw: 0,
    total: 0
  })

return (/*SOME HTML*/)

Also putting here my useTransaction hook: https://gist.github.com/gabrielforster/314e5a5f91d8f83c78a3cc8a56a55435

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Best to post the code for useTransactions(). Here I'm guessing that it is actually waiting for some data to complete. The best way to handle this is with a useEffect which fires when transactions is updated and put summary in a state. I added a optional chaining check .? to check for null or undefined before actually using reduce.

    const [summary, setSummary] = React.useState([])

    React.useEffect(() => {
        setSummary(transactions?.reduce(
            (acc, transaction) => {
                if (transaction.type === 'deposit') {
                    acc.deposit += transaction.amount;
                    acc.total += transaction.amount;
                } else {
                    acc.withdraw = transaction.amount;
                    acc.total -= transaction.amount;
                }

                return acc;
            },
            {
                deposit: 0,
                withdraw: 0,
                total: 0,
            },
        ));
    }, [transactions]);
about 4 years ago · Juan Pablo Isaza Report

0

It was an error on my useTransaction hook guys! Thank you all floks

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!