Commit 3da805f
authored
fix(plugin-multi-tenant): infinite api calling loop on user switch (#16065)
Previously, using the `multi-tenant` example you were able to reproduce
the following:
* Login as `super-admin` `demo@payloadcms.com` `demo`
* Logout and login as `tenant1` `tenant1@payloadcms.com` `demo`
* Logout and login as `tenant2` `tenant2@payloadcms.com` `demo`
You'd see an infinite spam of requests to
`/tenants/populate-tenant-options` because of `useEffect` in
`TenantSelectionProvider/index.client.tsx`
Why?
Login as Tenant 2 → userChanged=true → syncTenants() fetches data
→ setTenantOptions(new array) → tenantOptions reference changes
→ effect re-runs (tenantOptions in deps) → stale initialValue ≠ cookie
→ syncTenants() again → new array reference again → ∞
This PR makes the `setTenantOptions` call stable (avoid a new reference
when not changed) and removes `tenantOptions` from the dependency array
(the tenant options selector still updates correctly when logging out /
logging in as a different user)
---
- To see the specific tasks where the Asana app for GitHub is being
used, see below:
- https://app.asana.com/0/0/12138224648528641 parent d7c07b3 commit 3da805f
1 file changed
Lines changed: 2 additions & 4 deletions
Lines changed: 2 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
251 | 251 | | |
252 | 252 | | |
253 | 253 | | |
254 | | - | |
255 | | - | |
256 | | - | |
| 254 | + | |
257 | 255 | | |
258 | 256 | | |
259 | 257 | | |
260 | 258 | | |
261 | | - | |
| 259 | + | |
262 | 260 | | |
263 | 261 | | |
264 | 262 | | |
| |||
0 commit comments