Creating generic blocks for state control and display? #71839
TristanvWyk
started this conversation in
Interactivity API
Replies: 1 comment 5 replies
-
You can access context or state from other namespaces using:
To load the other stores, you need to load them first. So if you haven't loaded them yet, you'll need to import them. // Statically
import 'other-store-module-identifier';
// Or dynamically when needed;
store( 'my-plugin', {
actions: {
*someAction() {
yield import( 'other-store-module-identifier' );
const otherStore = store( 'other-plugin' );
if ( otherStore.state.something ) {
// You can also run other store actions
otherStore.actions.someAction();
}
}
} If you export the stores in your modules, then you can also directly import them, without having to use // Export the stores
export const { state, actions } = store( 'other-store', {...} ); // Then, import them in the other modules
import { state as otherState, actions as otherActions } from 'other-store-module-identifier';
// or dynamically
const { state, actions } = yield import( 'other-store-module-identifier' ); Let me know if that clarifies things for you or if there's anything that's not clear 🙂 |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi Everyone! I recently started making use of the interactivity API as part of a custom theme we are developing. I've loved what it has to offer so far but I've been struggling to maintain a DRY approach to development. I'm fairly new to WordPress and web development in general so it's likely that this issue stems simply from a gap in my own knowledge, but I've scoured the official documentation as well as this and other forums and haven't been able to find a direct solution to my predicament. I'm hoping someone here can shed some insight on what approach I should be taking for something like this.
The Problem:
I haven't found a way to share values across blocks that doesn't require both blocks to be locked into that particular namespace. For example, I recently put together a simple selector block and selector group that allows users to select an option within the group much like a radio button. This is a block I would like to use in various different contexts, such as selecting product variations in woo or having an option from one selection group restrict the options in another.
Locking the store to a single namespace doesn't seem correct as it will need to interact with the state of other stores. In certain instances they are wrapped in an accordion or tab group which also have their own interactivity namespaces, meaning I can't always rely on the parent to implement the functions and such.
I'm having a similar issue with an Incrementor block I made that accepts a minimum and maximum number and allows a user to select a value between those two numbers. The minimum and maximum need to be controlled by values in another store, but the value selected in the block should also be accessible to other stores that might want to make use of it. This is the current case for a custom woo commerce variable product page.
In summary the question is "What is the best approach for creating generic controller blocks that can be interfaced and interface with other store states?" Ideally in a way that is reusable and modular.
Current "Working" Solution:
At the moment, I've implemented the means for these generic elements to accept a callback method from a foreign store that wants to make use of them. The callback will listen for a change in context in the generic block and essentially update the foreign block's store to match and/or adjust any parameters of the Generic block. This implementation gets messy really quickly and feels more like a work around than an actual solution.
Any assistance or advice would be greatly appreciated, Hopefully I'm leaving this in the right place.
Beta Was this translation helpful? Give feedback.
All reactions