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

225
Views
"Uncaught TypeError: props.expenses.map is not a function" while setting state with useState hook

I am using react useState to make a demo. I am facing below error while I am trying to print my expenses data inside Expenses component. if I am passing data without useState it is working but if I am using useState it is not working.

Error

Uncaught TypeError: props.expenses.map is not a function
    at Expenses (Expenses.js:18:1)
    at renderWithHooks (react-dom.development.js:14985:1)
    at updateFunctionComponent (react-dom.development.js:17356:1)
    at beginWork (react-dom.development.js:19063:1)
    at HTMLUnknownElement.callCallback (react-dom.development.js:3945:1)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:3994:1)
    at invokeGuardedCallback (react-dom.development.js:4056:1)
    at beginWork$1 (react-dom.development.js:23964:1)
    at performUnitOfWork (react-dom.development.js:22776:1)
    at workLoopSync (react-dom.development.js:22707:1)

My code

import { useState } from "react";
import "./App.css";
import Expenses from "./components/Expenses/Expenses";
import NewExpense from "./components/NewExpense/NewExpense";

const DUMMY_DATA = [
  {
    id: "ex1",
    title: "expense item 1",
    amount: 120,
    date: new Date(2022, 2, 22),
  },
  {
    id: "ex2",
    title: "expense item 2",
    amount: 100,
    date: new Date(2022, 2, 23),
  },
  {
    id: "ex3",
    title: "expense item 3",
    amount: 90,
    date: new Date(2022, 2, 20),
  }
];

function App() {
  const [expenses, setExpenses] = useState(DUMMY_DATA);

  const onAddExpenseHandler = (expense) => {
    setExpenses((prevExpenses) => {
      return { expense, ...prevExpenses };
    });
  };

  return (
    <div className="App">
      <NewExpense onAddExpense={onAddExpenseHandler} />
      <Expenses expenses={expenses} />
    </div>
  );
}

export default App;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Problem was return type of state. I was returning object instead of array. I have set some dummy expenses data which is type of array. so returning array instead of object fixed my issue. As I was passing object it was not able to loop through array and was giving error.

setExpenses((prevExpenses) => {
   return { expense, ...prevExpenses }; //Problem here
});

Solution:

setExpenses((prevExpenses) => {
   return [expense, ...prevExpenses]; //use array over here
});
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!