-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
feat: Add new section on lifecycle of the block.
1 parent
e5dca54
commit 2855da8
Showing
4 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
172 changes: 172 additions & 0 deletions
172
docs/getting-started/fundamentals/lifecycle-of-block.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
# Lifecycle of a block | ||
|
||
A WordPress block goes through several distinct phases during its lifecycle, from registration to rendering. Understanding these phases is crucial for block development. | ||
|
||
## Overview of block lifecycle phases | ||
|
||
1. Registration phase | ||
2. Edit phase (Block Editor) | ||
3. Save phase | ||
4. Serialization phase | ||
5. Rendering phase | ||
|
||
### 1. Registration phase | ||
|
||
When WordPress loads, blocks go through a registration process: | ||
|
||
```js | ||
// Client-side registration | ||
registerBlockType('namespace/block-name', { | ||
// Block configuration | ||
}); | ||
|
||
// Server-side registration | ||
register_block_type('namespace/block-name', array( | ||
// Block configuration | ||
)); | ||
``` | ||
|
||
During registration: | ||
- Block metadata from `block.json` is loaded | ||
- Block assets are enqueued | ||
- Server-side and client-side handlers are registered | ||
- Block variations and styles are registered | ||
|
||
### 2. Edit phase | ||
|
||
The edit phase occurs within the Block Editor when users interact with the block: | ||
|
||
- Block is instantiated as a React component | ||
- Block attributes are initialized with default or saved values | ||
- Edit function renders the block's interface | ||
- Block controls and inspector panels become available | ||
- Changes are stored in the block's attributes | ||
|
||
*Example: Block edit lifecycle* | ||
```js | ||
edit: ({ attributes, setAttributes, isSelected }) => { | ||
// Component mount | ||
useEffect(() => { | ||
// Initialize block | ||
}, []); | ||
|
||
// User interactions | ||
const onChangeContent = (newContent) => { | ||
setAttributes({ content: newContent }); | ||
}; | ||
|
||
// Render block interface | ||
return ( | ||
<div> | ||
{/* Block content and controls */} | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
### 3. Save phase | ||
|
||
The save phase occurs when the post is saved: | ||
|
||
- Block's save function is called | ||
- Current attributes are used to generate block markup | ||
- For dynamic blocks (`save: null`), only the block comment is saved | ||
|
||
*Example: Block save function* | ||
```js | ||
save: ({ attributes }) => { | ||
return ( | ||
<div> | ||
{/* Static markup generated from attributes */} | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
### 4. Serialization phase | ||
|
||
During serialization, blocks are converted to their HTML storage format: | ||
|
||
```html | ||
<!-- wp:namespace/block-name {"attribute":"value"} --> | ||
<div class="wp-block-namespace-block-name"> | ||
Block content | ||
</div> | ||
<!-- /wp:namespace/block-name --> | ||
``` | ||
|
||
Key aspects of serialization: | ||
- Attributes are stored as JSON in the block comment | ||
- Block markup is validated against the save function output | ||
- Changes in markup structure require block deprecation handling | ||
|
||
### 5. Rendering phase | ||
|
||
The final phase where blocks are displayed on the frontend: | ||
|
||
For static blocks: | ||
- Saved markup is retrieved from the database | ||
- Markup is filtered through `render_block` filters | ||
- Final HTML is output to the page | ||
|
||
For dynamic blocks: | ||
- Block attributes are passed to the render callback | ||
- Server-side function generates fresh markup | ||
- Generated HTML is filtered and displayed | ||
|
||
*Example: Dynamic block render callback* | ||
```php | ||
function render_callback($attributes, $content) { | ||
$wrapper_attributes = get_block_wrapper_attributes(); | ||
|
||
return sprintf( | ||
'<div %1$s>%2$s</div>', | ||
$wrapper_attributes, | ||
esc_html($attributes['content']) | ||
); | ||
} | ||
``` | ||
|
||
## Block format transformations | ||
|
||
Throughout its lifecycle, a block exists in different formats: | ||
|
||
1. **JavaScript object format** | ||
- Used during registration and in the editor | ||
- Contains all block configuration and attributes | ||
|
||
2. **React component format** | ||
- Active in the Block Editor | ||
- Handles user interactions and UI rendering | ||
|
||
3. **HTML comment format** | ||
- Storage format in the database | ||
- Contains block name and attributes | ||
|
||
4. **HTML markup format** | ||
- Final rendered output | ||
- Either stored (static) or generated (dynamic) | ||
|
||
## Best practices | ||
|
||
1. **Validation** | ||
- Always validate data during format transformations | ||
- Handle deprecated block versions gracefully | ||
- Implement proper error handling | ||
|
||
2. **Performance** | ||
- Minimize unnecessary re-renders in the edit function | ||
- Use memoization for expensive computations | ||
- Implement proper cleanup in useEffect hooks | ||
|
||
3. **Compatibility** | ||
- Test block migrations thoroughly | ||
- Maintain backward compatibility | ||
- Follow WordPress coding standards | ||
|
||
## Additional Resources | ||
|
||
- [Block API Reference](https://developer.wordpress.org/block-editor/reference-guides/block-api/) | ||
- [Data Flow in the Block Editor](https://developer.wordpress.org/block-editor/explanations/architecture/data-flow/) | ||
- [Block Deprecation](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-deprecation/) | ||
- [Block Validation](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#validation) |
This file contains 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
This file contains 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