useSelect in block Edit always returns default store value

given my custom store, when I try to use readItems selector, I am always getting default store state. Selector works fine (tested in console with wp.data.select), however, it takes some time to catch values asynchronously I presume useSelect memoizes first returned value which is default store state and does not respond to store changes.

How to fix this?

Beginning of block edit function:

const LECTURERS_TABLE = 'tla_mit_lecturers';


export default function Edit(props) {
    const { attributes, setAttributes } = props;

    const allItems = useSelect(select => select('REST-extra_table').readItems(LECTURERS_TABLE)); 

Store:

import apiFetch from '@wordpress/api-fetch';
import { createReduxStore, register } from '@wordpress/data';

/**
 * returns table id from table name
 * @param  {} table
 */
function getTableId(table) {
    if (! table) {
        console.log('Table name not supplied for API');
        return undefined;
    }
    const tableId = tableIds.indexOf(table);
    if (tableId == -1) {
        console.log('Wrong table name supplied to the API');
        return undefined;
    }
    return tableId;
}

// items is array of tables - item['table_name'][index] is individual item
const tableIds = [
    'tla_mit_lecturers',
]

const DEFAULT_STATE = {
    tables: [
        ['default']
    ],
};

const actions = {
    *createItem( item, table ) {
        const tableId = getTableId(table);
        const retValue = yield actions.fetchFromAPI(
            {
                path: '/extra_table/v1/item?table=' + table,
                method: 'POST',
                data: item
            }
         );
        // retValue should be ID if insert is successful
        return retValue ?
            {
                type: 'CREATE_ITEM',
                item: {ID: retValue, ...item},
                tableId
            } :
            {
                type: 'FETCH_ERROR',
                error: 'unknown'
            };
    },

    *deleteItem( ID, table ) {
        const tableId = getTableId(table);
        const retValue = yield actions.fetchFromAPI(
            {
                path: '/extra_table/v1/item?table=' + table + '&ID=' + ID,
                method: 'DELETE'
            }
         );
        return retValue ?
            {
                type: 'DELETE_ITEM',
                tableId,
                ID
            } :
            {
                type: 'FETCH_ERROR',
                error: 'unknown'
            };
    },

    *updateItem( ID, item, table ) {
        const tableId = getTableId(table);
        const retValue = yield actions.fetchFromAPI(
            {
                path: '/extra_table/v1/item?table=' + table + '&ID=' + ID,
                method: 'PUT',
                data: item
            }
         );
        return retValue ?
            {
                type: 'UPDATE_ITEM',
                item: {ID, ...item},
                ID,
                tableId
            } :
            {
                type: 'FETCH_ERROR',
                error: 'unknown'
            };
    },

    // action to set item values - hydrate in https://unfoldingneurons.com/2020/wordpress-data-store-properties-resolvers
    setItem( item, tableId ) {
        return {
            type: 'SET_ITEM',
            item,
            tableId
        };
    },

    // action to set item values - hydrate in https://unfoldingneurons.com/2020/wordpress-data-store-properties-resolvers
    setAllItems( items, tableId ) {
        return {
            type: 'SET_ITEMS',
            items,
            tableId
        };
    },

    fetchFromAPI( action ) {
        return {
            type: 'FETCH_FROM_API',
            path: action.path,
                method: action.method,
                data: action.data,
                tableId: action.tableId
        };
    }
};

const store = createReduxStore( 'REST-extra_table', {
    reducer( state = DEFAULT_STATE, action ) {
        switch ( action.type ) {
            case 'CREATE_ITEM':
                return {
                    ...state,
                    tables: Object.assign([...state.tables], {[action.tableId]: [...state.tables[action.tableId], action.item] })
                };
            case 'UPDATE_ITEM':
                return {
                    ...state,
                    tables: Object.assign(
                            [...state.tables],
                            {[action.tableId]:
                                Object.assign(
                                    state.tables[action.tableId],
                                    {[state.tables[action.tableId].findIndex(el => el.ID === action.ID)]: action.item}
                                )
                    })
                };
            case 'DELETE_ITEM':
                return {
                    ...state,
                    tables: Object.assign([...state.tables], {[action.tableId]: state.tables[action.tableId].filter((el) => el.ID !== action.ID) })
                };
            // hydrate
            case 'SET_ITEM':
                return {
                    ...state,
                    tables: Object.assign([...state.tables], {[action.tableId]: [...state.tables[action.tableId], action.item] })
                }
            case 'SET_ITEMS':
                return {
                    ...state,
                    tables: Object.assign([...state.tables], {[action.tableId]: [...action.items] })
                }
            case 'FETCH_ERROR':
                console.log('AJAX fetch error, code: ' + action.error);
                return state;

        }
                // console.error('Table name is not given!!! '+action.type)

        return state;
    },

    actions,

    selectors: {
        readItem( state, ID, table ) {
            const tableId = getTableId(table);
            if (tableId === undefined)
                return undefined;
            if (state.tables[tableId])
                return state.tables[tableId].find(el => el.ID == ID);
            else
                return undefined;
        },
        readItems( state, table ) {
            const tableId = getTableId(table);
            if (tableId === undefined)
                return undefined;
            // return empty array if state.tables does not exist
            if (! state.tables[tableId])
                console.error('Requested table does not exist in state!');
            return state.tables[tableId] || undefined;
        },
    },

    controls: {
        FETCH_FROM_API( action ) {
            return apiFetch(
                {
                    path: action.path,
                    // path: action.path,
                    method: action.method,
                    credentials: 'same-origin',
                    mode:'same-origin',
                    // headers: {
                    //  'Content-Type': 'application/json'
                    //  },
                    data: action.data
                } );
        },
    },

    resolvers: {
        *readItem( ID, table ) {
            const tableId = getTableId(table);
            const item = yield actions.fetchFromAPI(
                {
                    path: '/extra_table/v1/item?table=' + table + '&ID=' + ID,
                    method: 'GET',
                    data: null,
                    tableId
                }
             );
            return item !== undefined ? actions.setItem( item, tableId ) : undefined;
        },
        *readItems( table ) {
            const tableId = getTableId(table);
            const allItems = yield actions.fetchFromAPI({
                    path: '/extra_table/v1/all_items?table=' + table,
                    method: 'GET',
                    data: null,
                    tableId
            });
            return allItems !== undefined ? actions.setAllItems( allItems, tableId ) : undefined;
        },
    },
} );

register( store );

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

useSelect in block Edit always returns default store value

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.