最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin3PV0评论

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;
Post a comment

comment list (0)

  1. No comments so far