I have a few hooks that fetch data using react query and do low-level calculations with that data. Another custom hook will use the output of that data to calculate a different value.
For example:
- Hook #1 fetches a list of future expenses for a user's rental property (e.g. furnace, roof, etc.).
- Hook #2 fetches the user's preferences, how far in the future expenses should be considered and saved for (e.g. only expenses that come due in the next 5 years are relevant).
- Hook #3 calls hooks #1 and #2 and returns the total amount that should be set aside each month to pay for future expenses.
Since hook #3 is dependent on the data from the first two hooks, I'm not exactly sure how to structure that code. React query returns data and loading state, so I'm currently consolidating loading state from hooks #1 and #2 and putting the calculation for the data from hook #3 in a conditional. But that's resulting in re-render loop errors.
Any idea why that might be?
const usePropertyTotalMonthlyCapex = (propertyId) => {
const [preferences, preferencesIsLoading, preferencesIsError, preferencesError] = useGetUserPreferences()
const [expenses, isLoading, isError, error] = useGetExpenses(propertyId)
const [capex, setCapex] = useState(0)
if (preferences && expenses) { // this seems to be causing a render loop error
if (expenses.length === 0) {
setCapex(0)
} else {
const { outlookLength } = preferences
const expenseValues = expenses?.map(expense => {
const { lifespan, age, replacementCost } = expense
if (lifespan - age > outlookLength) {
return 0
}
return replacementCost / ((lifespan - age) * 12)
})
setCapex(expenseValues.reduce((acc, init) => acc + init).toFixed(0))
}
}
return [capex, preferencesIsLoading && isLoading]