WordPress Interactivity API was officially released with WordPress 6.5. As I've been studying this new feature, I find it intriguing, but I'm somewhat perplexed about certain aspects, particularly regarding the use of context.
My main question is: What's the rationale behind using context in addition to state? Wouldn't using state alone be sufficient?
I understand that context is supposed to be local while state is global. However, it appears that we can access context using namespaces, as demonstrated in this example:
import { getContext } from "@wordpress/interactivity";
const otherPluginContext = getContext( "otherPlugin" );
Furthermore, it seems possible to create private states, as shown here:
const { state } = store("myPlugin/private", {
state: {
messages: [ "private message" ]
}
},
{ lock: true }
);
// The following call throws an Error!
store( "myPlugin/private", { /* store part */ } );
Given these capabilities, I'm curious about the specific advantages and use cases for context in the WordPress Interactivity API. Could someone clarify the distinctions between state and context, and explain scenarios where using context would be preferable to using state alone?