admin管理员组文章数量:1417070
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?
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?
1 Answer
Reset to default 3Yes, it's possible and you'd use wp.data.select( 'core/data' ).isResolving()
.
Example based on your code:
const MyComponent = withSelect(select => {
const { isResolving } = select( 'core/data' );
const query = { _fields: 'id,name,slug' };
return {
terms: select('core').getEntityRecords("taxonomy", "my_taxonomy", query),
isRequesting: isResolving( 'core', 'getEntityRecords', [ 'taxonomy', 'my_taxonomy', query ] )
};
})(props => {
if ( props.isRequesting ) {
return (
<div className="loading">
Loading...
</div>
);
}
return (
<ul>
{ props.terms.map( term => (<li key={ term.id }>{ term.name }</li>) ) }
</ul>
);
});
And you might also want to check the "edit" component for the Categories block.
本文标签: javascriptGet loading state of wp data selector
版权声明:本文标题:javascript - Get loading state of wp data selector 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745257441a2650185.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论