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:
- How can I prevent the error so that it is only included once?
- 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;
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744346561a4569720.html
评论列表(0条)