Home>
I am making a calculation application with React.
When I tried to use the map method on an array of objects, I got the following message:
TypeError: incomeItems.map is not a function
Applicable source code
IncomeExpense.js
import React from'react';
export const IncomeExpense = (incomeItems, expenseItems) =>{
const incomeAmounts = incomeItems.map(incomeItem =>incomeItem.amount);
const incomeTotal = incomeAmounts.reduce((acc, cur) =>{
return acc = acc + cur;
});
return (
Income
<p>{incomeTotal}</p>
Expense
<p>-5,000 yen</p>
)
}
AddItem.js
The state is set here.
import React, {useState} from'react';
export const AddItem = ({ inputText, setInputText, inputAmount, setInputAmount, incomeItems, setIncomeItems, expenseItems, setExpenseItems, type, setType}) =>{
const typeHandler = (e) =>{
setType(e.target.value)
console.log(type)
}
const inputTextHandler = (e) =>{
setInputText(e.target.value);
console.log(inputText)
};
const inputAmountHandler = (e) =>{
setInputAmount(e.target.value)
console.log(inputAmount)
}
const submitItemHandler = (e) =>{
e.preventDefault();
if (type ==='inc') {
setIncomeItems((
...incomeItems, {text: inputText, amount:inputAmount, id: Math.random() * 1000}
]);
} else if (type ==='exp') {
setExpenseItems((
...expenseItems, {text: inputText, amount:inputAmount, id: Math.random() * 1000 }.
]);
}
console.log(incomeItems)
}
return (
<>
<form>
<select onChange={typeHandler}>
<option value="inc">+</option>
<option value="exp" >-</option>
</select>
<label>Text</label>
<input type="text" placeholder="Text" value={inputText} onChange={inputTextHandler} />
<label>Amount</label>
<input type="number" placeholder="Value" value={inputAmount} onChange={inputAmountHandler} />
<button type="submit" onClick={submitItemHandler}>Add</button>
</form>
</>
)
}
Home.js
import React, {useState} from'react';
import {app} from "../firebase/Firebase";
import {Header} from'./Header';
import {Balance} from'./Balance';
import {IncomeExpense} from'./IncomeExpense';
import {AddItem }from'./AddItem';
import {ItemsList} from'./ItemsList';
function Home (props) {
const [inputText, setInputText] = useState("");
const [inputAmount, setInputAmount] = useState(0);
const [incomeItems, setIncomeItems] = useState([]);
const [expenseItems, setExpenseItems] = useState([]);
const [type, setType] = useState("inc");
return (
<Header />
<Balance />
<IncomeExpense
incomeItems={incomeItems}
expenseItems={expenseItems}
/>
<ItemsList
incomeItems={incomeItems}
setIncomeItems={setIncomeItems}
expenseItems={expenseItems}
setExpenseItems={setExpenseItems}
/>
<button onClick={() =>app.auth().signOut()}>log out</button>
)
}
export default Home;
What I tried
I have passed incomeItems as props from the main.
Why can't I use it even though it is an object array?
-
Answer # 1
Related articles
- javascript - i'm new to gulp gulp processing is not executed
- javascript - rails6 asynchronous like function implementation the display does not change
- javascript - js file not applied to html
- javascript - onsubmit does not run
- javascript - i'm making tetris with js, but i don't know how to resolve the error
- error in callback function in php class
- to call a javascript function on another page
- javascript - consolelog is not displayed
- javascript - button does not respond
- javascript does not work on the browser
- javascript - i want to create a function that returns the execution result of connectionquery
- i want to implement the accordion function with javascript
- javascript - i want to use the js code published on another site, but i get an error
- i want to change the key name part of an associative array in a javascript function
- error in vba split function
- python - i want to get rid of the error that the function is defined but not defined
- javascript is not reflected in the html file
- javascript - i want to implement the vuejs ranking function display in order of likes
- javascript - i got the error these relative modules were not found while using vue router
- javascript - cors error when accessing the server set up by express from the server set up by create-react-app cannot be resolve
↓
*Unverified