I have a custom block that shows the assigned taxonomy terms for the current post. I’m trying to show a loading spinner while those terms are loading in the background.
Let’s say I have a selector for a custom taxonomy that looks something like this:
const { withSelect } = wp.data;
const MyComponent = withSelect(select => {
return {
terms: select('core').getEntityRecords("taxonomy", "my_taxonomy", {
_fields: 'id,name,slug'
}),
}
})(props => {
console.log("taxonomies: ", props.terms);
return (
<ul>
{props.terms.map(t => (
<li key={term.id}>{term.name}</li>
))}
</ul>
);
});
When the editor loads and the taxonomy terms are fetched via some AJAX call the props.terms
value changes to the following values during that time
- initial: props.terms =
[]
- at some point after the request fires: props.terms =
null
- when the data is returned: props.terms =
[term1, term2, ...]
I guess that whenever props.terms === null
I could assume that the terms are loading, but I would prefer some kind of loading state from the store instead. Is that possible?