Performance: Block attributes and controls
This affects page load speed and interaction responsiveness by controlling how block data is stored and updated in the editor and frontend.
const attributes = { content: { type: 'string' } };
function Edit(props) {
const { attributes, setAttributes } = props;
const [localContent, setLocalContent] = React.useState(attributes.content);
React.useEffect(() => {
const timeout = setTimeout(() => setAttributes({ content: localContent }), 300);
return () => clearTimeout(timeout);
}, [localContent]);
return <textarea value={localContent} onChange={e => setLocalContent(e.target.value)} />;
}const attributes = { content: { type: 'string' } };
function Edit(props) {
const { attributes, setAttributes } = props;
return <textarea value={attributes.content} onChange={e => setAttributes({ content: e.target.value })} />;
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Immediate attribute update on every input | High (many updates) | High (many reflows) | High (many paints) | [X] Bad |
| Debounced attribute update with local state | Low (fewer updates) | Low (fewer reflows) | Low (fewer paints) | [OK] Good |
title of type string in a block's attributes object?type property as a lowercase string. Compare options: \"title\": { type: \"string\" } uses { type: \"string\" } which is correct. \"title\": { type: String } uses String (capitalized) which is invalid.const [content, setContent] = useState('Hello');
return (
setContent(value)}
/>
);useState hook holds the current content. The TextControl shows this content and updates it on user input. Follow the onChange handler: When user types \"World\", onChange calls setContent(\"World\"), updating the state and re-rendering the control with new value.attributes: {
count: { type: 'number', default: 0 }
} setAttributes({ count: value })}
/>onChange often passes a string from input controls. Identify mismatch causing update failure: Passing a string instead of a number causes the attribute not to update properly because types differ.value prop should be a number, but onChange passes a string -> Option AisActive. Which code snippet correctly defines the attribute and control to toggle this value?attributes: { isActive: { type: 'boolean', default: false } }
setAttributes({ isActive: value })}
/> correctly defines isActive as boolean with default false, which is standard for toggles. Use ToggleControl with checked and onChange: The checked prop uses the attribute value, and onChange updates the attribute with the new boolean value passed by the toggle.Pass the test and this pattern moves unknown