const [state, setState] = useState({Items : [{
id: 1,
amount: 1,
},
{
id: 2,
amount: 1,
},
{
id: 3,
amount: 1,
},
{
id: 4,
amount: 1,
},],
totalItems : (function () {
let total = 0;
for (let x of this.Items){
total += x.amount
}
return total
})()
});
I want to initialize the state’s ‘totalItems’ property by calculating the total amount of items depending upon the ‘Items’ property of the same object. I had tried this function but it doesnt work. Throws error: Uncaught TypeError: Cannot read properties of undefined (reading ‘Items’).
Can someone explain me why this error occured, as well as the solution to my problem?
2
Answers
The reason that the error occurred, is that you cannot access
Items
using this default function. Instead, if you use the new ECMAScript 2015get
syntax, you can implement the functionality you require.You can now access this value as usual, using
state.totalItems
.Make the business logic outside of the initial state.
Create an object and call the function like
const yourObj = yourFn()
. Then simply set the state withyourObj
likeconst [state, setState] = useState(yourObj)
. States should be just simple values to store, not values depending on some calculator functions and not complex structures.I’m assuming your state’s dependency is not limited with initial phase and will depend on other state. So you can consider about using useEffect depends on your other state. With this way you can make them sync. Like;