我正在為 React Practice 構建應用程序,但在嘗試將對象數組存儲到本地存儲時遇到錯誤。我是 React 的初學者,所以我不確定發生了什么。我在 IncomeOutputList 組件中收到錯誤,因為我試圖使用數組列出一堆對象。這是我的代碼:應用組件:import React, { useState, useReducer } from 'react'; import BudgetInput from './components/input/BudgetInput'; import IncomeOutputList from './components/output/IncomeOutputList'; import IncomeOutput from './components/output/IncomeOutput';const useSemiPersistentState = (key, initialState) => { const [value, setValue] = React.useState( localStorage.getItem(key) || initialState ); React.useEffect(()=>{ localStorage.setItem(key, JSON.stringify(value)); }, [value, key]) return [value, setValue];};const App = () => { const [incomes, setIncomes] = useSemiPersistentState('income',[{}]); const [description, setDescription] = useState(''); const [type, setType] = useState('+'); const [value, setValue] = useState(''); const incomeObj = { desc: description, budgetType: type, incomeValue: value } const handleIncomeObjArray = () => { setIncomes(incomes.concat(incomeObj)); console.log(incomes + "testing"); } const handleChange = (event) => { //this handler is called in the child component BudgetInput setDescription(event.target.value); } const handleSelectChange = (event) => { //this handler is called in the child component BudgetInput setType(event.target.value); } const handleValueChange = (event) => { setValue(event.target.value); console.log(incomeObj) } return ( <div className="App"> <div className="top"> </div> <div className="bottom"> <BudgetInput descValue={description} onDescChange={handleChange} onSelectChange={handleSelectChange} type={type} onBudgetSubmit={handleIncomeObjArray} budgetValue={value} onValChange={handleValueChange} /> {/* <IncomeOutput obj={incomeObj} /> */}
TypeError: list.map 不是一個函數
月關寶盒
2023-03-24 14:58:07