I want to set both of these values in the state to be used later as variables.
JavaScript
x
10
10
1
const [dollarGoal, setDollarGoal] = React.useState(0);
2
const [dollarValue, setDollarValue] = React.useState(0);
3
//fetching donation value
4
fetch('api', {
5
method: 'GET',
6
})
7
.then(response => response.json())
8
.then(r => setDollarValue(r.dollarValue))
9
.then(r => setDollarGoal(r.dollarGoal));
10
But it gives the error:
JavaScript
1
2
1
Property 'dollarGoal' does not exist on type 'void'.
2
How do I fix this?
Advertisement
Answer
Try this:
JavaScript
1
9
1
fetch('api', {
2
method: 'GET',
3
})
4
.then(response => response.json())
5
.then((r) => {
6
setDollarValue(r.dollarValue)
7
setDollarGoal(r.dollarGoal)
8
});
9
The last two .then
statements need to be combined. By calling .then
twice, you’re sending the result of setDollarValue()
(a void function) to the next .then
, which is not what you want.