Last active
July 8, 2025 01:58
-
-
Save kjroelke/68badc14430a03e782ea9280dfff6e2a to your computer and use it in GitHub Desktop.
Creating a Custom Store in WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { createReduxStore, register } from '@wordpress/data'; | |
| type DispatchActions = { | |
| type: 'ADD_POST' | 'REMOVE_POST'; | |
| postId: number; | |
| }; | |
| export const STORE_NAMESPACE = 'choctaw-print/featured-work'; | |
| const INITIAL_STATE: Set< number > = new Set(); | |
| export const store = createReduxStore( STORE_NAMESPACE, { | |
| reducer: (state = INITIAL_STATE, action: DispatchActions): Set< number > => { | |
| switch ( action.type ) { | |
| case 'ADD_POST': { | |
| const newState = new Set( state ); | |
| newState.add( action.postId ); | |
| return newState; | |
| } | |
| case 'REMOVE_POST': { | |
| const newState = new Set( state ); | |
| newState.delete( action.postId ); | |
| return newState; | |
| } | |
| default: | |
| return state; | |
| } | |
| }, | |
| actions: { | |
| addPost( postId: number ) { | |
| return { | |
| type: 'ADD_POST', | |
| postId, | |
| } | |
| }, | |
| removePost( postId: number ) { | |
| return { | |
| type: 'REMOVE_POST', | |
| postId, | |
| } | |
| } | |
| }, | |
| selectors: { | |
| getFeaturedWork( state: Set< number > ) { | |
| return state; | |
| }, | |
| }, | |
| } ); | |
| register( store ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment