• In the code below, I’m using useSelect to pull in userData and then pass it on to my component. The problem I am having is that it looks like the component is running multiple times. First couple of times console.log(userData); is empty before returning a an array on the second call.

    I am looking for a single call so I can populate the option values in the Select Control.

    const { __ } = wp.i18n;
    const { compose } = wp.compose;
    const { withSelect, useSelect, withDispatch } = wp.data;
     
    const { PluginDocumentSettingPanel } = wp.editPost;
    const { SelectControl } = wp.components;
    
    const TTI_Sidebar_Component_Plugin = ( { postType, postMeta, setPostMeta, userData} ) => {
    	if ( 'post' !== postType ) return null;  // Will only render component for post type 'post'
    	console.log(userData);
    	return(
    		<PluginDocumentSettingPanel title={ __( 'Secondary Authors') } icon="edit" initialOpen="true">
          <SelectControl multiple={true} label={__('Select secondary authors')} 
            options={ [
                { value: 'a', label: 'User A' },
                { value: 'b', label: 'User B' },
                { value: 'c', label: 'User c' },
            ] }
          />
    		</PluginDocumentSettingPanel>
    	);
    }
     
    export default compose( [
    	withSelect( ( select ) => {		
    		const query = { per_page: 3 };
    		return {
    			postMeta: select( 'core/editor' ).getEditedPostAttribute( 'meta' ),
    			postType: select( 'core/editor' ).getCurrentPostType(),
    			userData: select( 'core' ).getEntityRecords( 'root', 'user', query ),
    		};
    	} ),
    	withDispatch( ( dispatch ) => {
    		return {
    			setPostMeta( newMeta ) {
    				dispatch( 'core/editor' ).editPost( { meta: newMeta } );
    			}
    		};
    	} )
    ] )( TTI_Sidebar_Component_Plugin );
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Component With useSelect Runs Multiple Times’ is closed to new replies.