I am trying to add some html from one of my functions the edit and save funktion. If I do it manually it works:
return <p>Hello World</p>;
But if I do it with a function that returns a dom element, I get this error:
Gutenberg Block: Objects are not valid as a React child (found: [object HTMLDivElement]).
But if I do a console.info chrome displays the html. Any idea why this does not work?
registerBlockType( 'simpletoc/toc', {
title: __( 'SimpleTOC', 'simpletoc' ),
icon: listul,
category: 'layout',
edit: function( props ) {
const data = wp.data.select( 'core/block-editor' );
const blocks = data.getBlocks();
console.info(generateTOC(blocks));
return generateTOC(blocks);
},
save: ( props ) => {
return generateTOC(blocks);
},
} );
function generateTOC(blocks){
var div = document.createElement('div');
var ul = document.createElement('ul');
var h2 = document.createElement('h2');
var headline = __( 'Table of Contents', 'simpletoc' );
h2.appendChild(document.createTextNode(headline));
blocks.forEach(function (item,index){
var blockId = '';
var slug = '';
h2 = document.createTextNode(headline)
var title = '';
if ( item['name'] === 'core/heading' ) {
blockId = (item['clientId']);
title = item.attributes.content;
slug = item.attributes.content.toSlug();
var li = document.createElement('li');
li.appendChild(document.createTextNode(title));
ul.appendChild(li);
}
});
div.appendChild(h2);
div.appendChild(ul);
return div;
}
setHeadingAnchors
do?