I’ve a situation the place I should use the identical API key for all of the blocks that’s current and that will likely be created new. So when person inputs on one block via a type, it would replace on all of the blocks of the namespace immediately.
At the moment when register_block_type()
and props.setAttributes
methodology is used, the values for every attribute is exclusive for every of the block. I’ve seen the documentation. I could not see a regular approach for sharing the identical worth throughout blocks.
Register code:
registerBlockType("namespace", {
title: "Plugin title",
class: "widespread",
attributes: {
apiKey: { kind: "string" },
Dealing with submit from the shape inside InspectorControls:
<InspectorControls>
<PanelBody>
<type id="api-key-input" onSubmit={(e) => handleSubmit(e)}>
<enter
kind="password"
ref={inputRef}
defaultValue={props.attributes.apiKey}
/>
<enter kind="submit" />
</type>
</InspectorControls>
perform handleSubmit(e) {
e.preventDefault();
props.setAttributes({ apiKey: inputRef.present.worth });
}
The strategies that I’ve checked out to date.
- Storing the attributes within the database utilizing
update_option
however I didn’t see anyget_option
for the Gutenberg block. wp.knowledge.subscribe
approach of dispatching to the blocks required.- Making a registry retailer and managing the general worth utilizing states.
Is there an best/most secure approach of doing this that I’m lacking, like setting an possibility or helps worth?