apiFetch async causing thousands of renders
-
I am creating/developing a new plugin for which I am leveraging an external data source. I have created a custom store in support of this and have a resolver that does an api-fetch. I have discovered that the block is performing tens of thousands of renders during the await period and is predictably killing performance. I know this because I am counting my renders and are all occruing during that fetch period.
I have used the same exact pattern for my store that is in the WordPress Core Data store. Note that I have created console.log messages to track both the current position of execution as well as to provide placeholders for additional functionality. The code follows:
import { addQueryArgs } from "@wordpress/url"; import apiFetch from "@wordpress/api-fetch"; export const getTable = (id) => async ({ dispatch }) => { console.log(' ...Resolver - Before fetch'); try { const path = addQueryArgs('dynamic-tables/v1/table?tableId=' + id); console.log(' ...Resolver - API Call - ' + path); const table = await apiFetch({ path }); console.log(' ...Resolver - After fetch - returned table - ' + JSON.stringify(table)); console.log(table); dispatch.receiveTable(table, id); } catch (error) { console.log(' ...Resolver - async error - ' + error); } console.log(' Resolver - async completed'); }
One thing I have noted is that some (but not all) core resolver definitions include a scheme to support “lock/unlock”. I have not included this because that code is marked not only as experimental, but labeled in such a way that suggests it is guaranteed to break on upgrades.
I would be very appeciative of any help that can be provided, either by way of something to add OR in suggesting an alternate development pattern. Any changes I would make would be trying to find a needle in a haystack because I don’t know the cause of the renders. It could be that my code does not implement React/Redux properly. However, I think it more likely that there are things going on within the Editor that, when combined with my store, create unknown side effects.
- The topic ‘apiFetch async causing thousands of renders’ is closed to new replies.