Get loading state of wp data selector

Yes, 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.

Leave a Comment