createBlock()とは?@wordpress/blocks

createBlock()とは、新しいブロックインスタンスを作成するための関数です。@wordpress/blocksからimportして利用します。

import { createBlock } from '@wordpress/blocks';

基本的な構文

型と属性を指定してブロック・オブジェクトを返します。

createBlock(name, attributes, innerBlocks)

パラメータ

name

String

作成するブロックの名前。例: ‘core/paragraph’

attributes

Object

ブロックの属性を定義するオブジェクト。

innerBlocks

?Array

子ブロックの配列

戻り値

Object

新しく作成されたブロックオブジェクト。

ソース

引用元:WordPress/gutenberg · GitHub

/**
 * Returns a block object given its type and attributes.
 *
 * @param {string} name        Block name.
 * @param {Object} attributes  Block attributes.
 * @param {?Array} innerBlocks Nested blocks.
 *
 * @return {Object} Block object.
 */
export function createBlock( name, attributes = {}, innerBlocks = [] ) {
	const sanitizedAttributes = __experimentalSanitizeBlockAttributes(
		name,
		attributes
	);

	const clientId = uuid();

	// Blocks are stored with a unique ID, the assigned type name, the block
	// attributes, and their inner blocks.
	return {
		clientId,
		name,
		isValid: true,
		attributes: sanitizedAttributes,
		innerBlocks,
	};
}