Gutenberg & React Interview Questions

Assessing proficiency with the Block Editor, React, and Modern JS in WordPress.

The Block Editor (Gutenberg) is the present and future of WordPress. These questions test the ability to build custom blocks and extend the editor.

1. What is the difference between Static and Dynamic Blocks?

Answer:

  • Static Blocks: The HTML is generated in JavaScript (the `save` function) and saved directly into the `post_content` in the database. If you change the HTML structure later, existing blocks may break (validation error).
  • Dynamic Blocks: The `save` function returns `null`. The HTML is rendered on the server-side via PHP (`render_callback`). This is ideal for content that changes frequently (e.g., "Latest Posts") or complex logic.

2. How do you register a custom block?

Answer: Modern registration uses `block.json` metadata file.

  1. Create `block.json` defining name, attributes, and scripts.
  2. Register it in PHP:
function register_my_block() {
    register_block_type( __DIR__ . '/build' );
}
add_action( 'init', 'register_my_block' );

3. Explain Block Attributes and how they relate to React State.

Answer: Attributes are the data model for a block. They are similar to React state, but they persist to the database.

  • They are defined in `block.json`.
  • They are passed as props to the `Edit` component.
  • Updating them via `setAttributes` triggers a re-render of the editor UI.
"attributes": {
    "content": {
        "type": "string",
        "source": "html",
        "selector": "p"
    }
}

4. What is `wp.data` and how is it used?

Answer: `wp.data` is a centralized data registry built on top of Redux. It manages the state of the editor.

  • `useSelect`: Retrieves data (like `useSelector` in Redux). E.g., getting the current post title.
  • `useDispatch`: Dispatches actions (like `useDispatch` in Redux). E.g., updating a post meta value.
const { title } = useSelect( ( select ) => {
    return {
        title: select( 'core/editor' ).getEditedPostAttribute( 'title' )
    };
} );

5. How do you create a custom sidebar plugin for the Block Editor?

Answer: You use the `PluginSidebar` component from `@wordpress/edit-post`.

import { registerPlugin } from '@wordpress/plugins';
import { PluginSidebar } from '@wordpress/edit-post';

const MySidebar = () => (
    <PluginSidebar name="my-sidebar" title="My Settings">
        <div>Sidebar Content Here</div>
    </PluginSidebar>
);

registerPlugin( 'my-sidebar-plugin', {
    render: MySidebar,
} );

6. What are Block Patterns and how do they differ from Reusable Blocks?

Answer:

  • Block Patterns: Pre-defined layouts of blocks that users can insert. Once inserted, the blocks are independent; changing the pattern definition does not update existing instances. They are best for starting points.
  • Reusable Blocks (Synced Patterns): A saved group of blocks where changes sync across all instances on the site. They are best for global content like a Call-to-Action.

7. Explain `Block Context` and when you would use it.

Answer: Block Context allows a parent block to pass values down to its inner blocks (descendants) without passing them explicitly through every level of the hierarchy (prop drilling).

Example: A "Query Loop" block provides the `postId` context. An inner "Post Title" block consumes that context to know which title to display.

// Parent block.json
"providesContext": {
    "myPlugin/recordId": "recordId"
}

// Child block.json
"usesContext": [ "myPlugin/recordId" ]

8. What is the difference between `count` and `for_each` in Terraform? (Wait, wrong guide - keeping it WP focused) -> What are Block Variations?

Answer: Block Variations allow you to create different versions of a single block type. They share the same implementation but have different initial attributes or inner blocks.

Example: The core "Embed" block has variations for YouTube, Twitter, Vimeo, etc. They are all the same block, just pre-configured differently.

9. How do you handle "Deprecation" in Gutenberg blocks?

Answer: When you change the HTML structure of a block's `save` function, existing blocks in the database will break (validation error). To fix this, you define a `deprecated` array in the block registration.

This array contains the old `save` function and attributes. Gutenberg checks this list to see if the invalid content matches an old version. If it does, it successfully migrates it to the new version.

10. What is the `ServerSideRender` component?

Answer: It is a React component that fetches the PHP-rendered HTML of a block and displays it in the editor. It is useful for dynamic blocks where replicating the rendering logic in JavaScript is too complex or impossible.

import ServerSideRender from '@wordpress/server-side-render';

export default function Edit( props ) {
    return (
        <ServerSideRender
            block="my-plugin/latest-posts"
            attributes={ props.attributes }
        />
    );
}