• rice_crisp

    (@rice_crisp)


    I’m trying to create a block with an InnerBlocks template. Since the block has a template, the block should always have at least 1 child. If the child count ever reaches 0, then I want to remove this block, since it would be empty.

    The issue that I’m running into is that the value of “blockCount” is 0 for a split second when the block is first created, so it’s removing itself before it’s created. Is this a bug with getBlockCount, or is there a way to wait for the template to populate the InnerBlocks before I check the block count?

    Here is my edit function for reference.

    edit: props => {
      const { setAttributes } = props
      const { className } = props.attributes
      const { removeBlock } = useDispatch('core/block-editor')
      const { blockCount } = useSelect(select => ({
        blockCount: select('core/block-editor').getBlockCount(props.clientId)
      }))
      if (blockCount === 0) {
        removeBlock(props.clientId)
      }
      return (
        <div className={ className }>
          <InnerBlocks
            allowedBlocks={ ['my/block'] }
            template={ [['my/block']] }
            templateLock={ false }
            renderAppender={ false }
          />
        </div>
      )
    }
    • This topic was modified 5 years ago by rice_crisp.
    • This topic was modified 5 years ago by rice_crisp.
    • This topic was modified 5 years ago by rice_crisp.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘InnerBlocks with template returning a block count of zero’ is closed to new replies.