reactjs - How can I make sure my custom store is only registered once in WordPress? - Stack Overflow

I created a custom-store.js file to fetch data from an API using a custom store. This store is used acr

I created a custom-store.js file to fetch data from an API using a custom store. This store is used across multiple components.

However, in the frontend console, I keep seeing this warning multiple times:

data.min.js?ver=7c62e39…:2 Store "custom-store/counter" is already registered.
data.min.js?ver=7c62e39…:2 Store "custom-store/counter" is already registered.
...

How can I ensure that custom-store.js is only imported or registered once?

In the components where I fetch the data, I use the following code:

import counterStore from '../../store/custom-store.js';

const recipes = useSelect((select) => select(counterStore).getRecipe());

What is the best way to avoid multiple store registrations across components?

In my custom-store.js

import { createReduxStore, register } from '@wordpress/data';
...

const STORE_NAME = 'custom-store/counter';

...
...

const store = createReduxStore(STORE_NAME, {
    reducer,
    actions,
    selectors,
});

// This has not work:
const checkStore = select(STORE_NAME);
if (checkStore && typeof checkStore.getIcons !== 'function') {
    register(store);
}

register(store);

export default store;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743770673a4504315.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信