I created a custom useOrganisation hook to fetch and manipulate data from my API which look something along the lines of:
export function useOrganisation({ organisationId }) { const [organisation, setOrganisation] = useState() // Load organisation useEffect(() => { loadOrganisation({ organisationId, setOrganisation }) }, [organisationId]) return { organisation, setOrganisation } } async function loadOrganisation({ organisationId, setOrganisation }) { const data = await fetchOrganisationData(organisationId) setOrganisation(data) } This works well when I call it from within a component where I need to display information for a single organisation, because I use the hook to fetch that one specific organisation, however, when I want to use it on a dashboard-like page to load multiple organisations simultaneously, I'm unable to do so since I cannot call the hook from within a look like such:
export default function OrganisationsDashboard() { const [organisations, setOrganisations] = useState([]) const organisationIds = ['org1', 'org2', 'org3'] organisationIds.forEach(orgId => { setOrganisations(prevState => { const { organisation } = useOrganisation(orgId) const stateCopy = [...prevState] copy.push(organisation) return copy }) }) } What is the correct way to re-use the hook logic when needing to load multiple organisations into an array like in the mock component above?