最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Add store to custom wordpress plugin?

programmeradmin7浏览0评论

I'm currently working on a custom WordPress plugin that includes multiple custom blocks.

The folder structure looks like this:

src/
- custom-block-one
- custom-block-two
- custom-block-three
- custom-block-four

The files are loaded in the build:

build/
- custom-block-one
- custom-block-two
- custom-block-three
- custom-block-four

I would like to integrate a Redux store.

The files are located in the store folder in the root directory, alongside build and src:

build/
src/
store/  <= Redux Store

When I try to include my store in one of the four blocks (edit.js), I get the message:

Store 'my-plugin/custom-storage' is already registered.

I only added this line:

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

Now, my question is:

  1. How can I prevent the error so that it is only included once?
  2. How can I create a global main.js file?

If I try to create one inside src/:

src/
- custom-block-one
- custom-block-two
- custom-block-three
- custom-block-four
- main.js  <- Global files

It is not being built.

This is the custom-store.js

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

const STORE_NAME = 'custom-block/counter';

// Definiere den anfänglichen Zustand
const DEFAULT_STATE = {
    count: 0,
};

// Definiere die Aktionen
const actions = {
    increment() {
        return { type: 'INCREMENT' };
    },
    decrement() {
        return { type: 'DECREMENT' };
    },
    setCount(value) {
        return { type: 'SET_COUNT', value };
    },
};

// Definiere den Reducer
function reducer(state = DEFAULT_STATE, action) {
    switch (action.type) {
        case 'INCREMENT':
            return { ...state, count: state.count + 1 };
        case 'DECREMENT':
            return { ...state, count: state.count - 1 };
        case 'SET_COUNT':
            return { ...state, count: action.value };
        default:
            return state;
    }
}

// Definiere die Selektoren
const selectors = {
    getCount(state) {
        return state.count;
    },
};

// Erstelle und registriere den Store
const store = createReduxStore(STORE_NAME, {
    reducer,
    actions,
    selectors,
});

register(store);

export default store;
发布评论

评论列表(0)

  1. 暂无评论