I think I'm not understanding something fundamental about React hooks or maybe even just React in general. I'm setting values from the backend.
const [values, setValues] = useState({
rentAmount: props.values.zestimates.rent_zestimate.toLocaleString('en-US'),
monthlyPayment: props.values.zestimates.rent_zestimate.toLocaleString('en-US'),
vacancyAmount: (props.values.zestimates.rent_zestimate * 0.1).toLocaleString('en-US'),
vacancyRate: 10,
repairsAmount: (props.values.zestimates.rent_zestimate * 0.1).toLocaleString('en-US'),
repairsRate: 10,
propertyManagementAmount: (props.values.zestimates.rent_zestimate * 0.1).toLocaleString('en-US'),
propertyManagementRate: 10,
})
The user can change rentAmount from a textbox as well as change the rates from other textboxes and what I want is for 3 different values to update once rentAmount has changed but I'm running into an issue where the last declared setValues() is the only one to run.
I have 5 useEffects set up (probably not the way to do this but kind of stumbled across React Hooks halfway through this hobby project) but I'm focusing on just 3 for this question
useEffect(() => {
setValues({ ...values, vacancyAmount: formatter.format(values.rentAmount.toString().replace(/,/g, '') * values.vacancyRate/100).replace('$', '')})
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [values.vacancyRate, values.rentAmount])
useEffect(() => {
setValues({ ...values, repairsAmount: formatter.format(values.rentAmount.toString().replace(/,/g, '') * values.repairsRate/100).replace('$', '')})
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [values.repairsRate, values.rentAmount])
useEffect(() => {
setValues({ ...values, propertyManagementAmount: formatter.format(values.rentAmount.toString().replace(/,/g, '') * values.propertyManagementRate/100).replace('$', '')})
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [values.propertyManagementRate, values.rentAmount])
My issue is, the last setValues for propertyManagementAmount runs just fine when rentAmount is updated but the other 2 don't run and I don't understand why.