首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Render Block from a array of Block- Gutenberg

Render Block from a array of Block- Gutenberg
EN

Stack Overflow用户
提问于 2019-11-30 00:16:50
回答 2查看 439关注 0票数 2

我需要以某种方式从我使用以下函数获得的块列表中渲染块:

代码语言:javascript
复制
const applyWithSelect = withSelect((select, blockData) => {
    const parentClientId = select('core/block-editor').getBlockRootClientId(
        selectedClientId
    );

    return {
        innerBlocks: select('core/block-editor').getBlocks(blockData.clientId),
    };
});

因此,在这里,我将innerBlocks作为页面上块数组,如下所示(第一个元素):

代码语言:javascript
复制
0:
attributes: {title: "Enter Title", review: "Enter review", rating: "Add Rating", reviewerName: "Enter Name", reviewDate: "Enter Date", …}
clientId: "2413142124"
innerBlocks: []
isValid: true
name: "something/some-item"
originalContent: "<div>something</div>"

有没有办法让我在编辑函数中使用这个innerBlocks变量,然后以某种方式呈现这个块?不使用<InnerBlocks >的原因是我必须一个接一个地渲染它们,所以每个块在我的滑块中都是一个单独的元素。我需要这样的东西:

代码语言:javascript
复制
const reviews = this.props.innerBlocks;

return (
   <div>
       <div className="carousel">
           <Slider {...slickSettings} className={classnames(this.props.className)}>
               { reviews.map((block, i) => {
                    return (
                        block.render() // this line is the issue, doesn't have to be render, but
                                       // any other way of rendering block separately from InnerBlocks
                         )

                 })
               }
           </Slider>
       </div>
   </div>
)
EN

回答 2

Stack Overflow用户

发布于 2020-06-12 00:53:31

我一直在努力寻找类似的东西已经有一段时间了。我需要的只是用renderToString返回一个渲染过的块的字符串,但我认为你可以很容易地删除它,只返回块本身。

在深入研究WordPress's repo之后,我认为答案必须与函数getSaveElement( nameOfBlock, attributesObject, [optionalInnerBlocks] )有关

我的代码看起来像这样:

代码语言:javascript
复制
import { registerBlockType, getSaveElement } from '@wordpress/blocks';
import { renderToString } from '@wordpress/element';

const arrayOfInnerBlocks = something.innerBlocks; //for example purposes
const getRenderedEl = ( attributes ) => {
    const el = renderToString(
        getSaveElement( 'core/cover', attributes ) //this is the main thing!
    );
    return el;
}

// i'm deconstructing the innerBlock item and just getting attributes.
const blockElements = arrayOfInnerBlocks.map( {attributes} => ({ individualBlock: getRenderedEl(attributes) }) )

所以对你来说,就像这样:

代码语言:javascript
复制
const reviews = this.props.innerBlocks;
const getRenderedEl = ( {attributes, name} ) => getSaveElement( name, attributes ); //this is the main
return (
   <div>
       <div className="carousel">
           <Slider {...slickSettings} className={classnames(this.props.className)}>
               { reviews.map( getRenderedEl ) }
           </Slider>
       </div>
   </div>
)

希望这就是你要找的东西!

票数 0
EN

Stack Overflow用户

发布于 2021-10-30 09:59:43

您可以使用BlockEditorProvider,在编辑函数中显示块,使用map函数列出所有数组,并在其中显示值属性中的元素。如果需要,您可以检测onChangeonInput中的更改。

代码语言:javascript
复制
<BlockEditorProvider
    value={ block[ index ] }
    onInput={ ( state ) => {
        console.log( state );
    } }
    onChange={ ( state ) => {
        const newBlockContent = block.concat( [] );
        newBlockContent[ index ] = state;
        setAttributes( {
            myBlocks: newBlockContent,
        } );
    } }
>
    <SlotFillProvider>
        <BlockTools>
            <WritingFlow>
                <ObserveTyping>
                    <BlockList />
                </ObserveTyping>
            </WritingFlow>
        </BlockTools>
        <Popover.Slot />
    </SlotFillProvider>
</BlockEditorProvider>;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59108152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档