0
votes

Gutenberg is still pretty new, but I'm still hoping someone has encountered this issue and found a solution.

I've used create-guten-block to boilerplate a project and created a tabbed-content block. I am using react-sortable-hoc with Gutenberg to swap the items from the item list. I tried several solutions but not getting the answer. The problem I'm running into is I am getting this error in Console that it cannot read map of Undefined. I think the undefined is items object.:

react-dom.min.fa9ca8c8.js:117 TypeError: Cannot read property 'map' of undefined
    at eval (block.js?921d:73)
    at Td (react-dom.min.fa9ca8c8.js:82)
    at hi (react-dom.min.fa9ca8c8.js:102)
    at Qg (react-dom.min.fa9ca8c8.js:144)
    at Rg (react-dom.min.fa9ca8c8.js:145)
    at Sc (react-dom.min.fa9ca8c8.js:158)
    at Z (react-dom.min.fa9ca8c8.js:156)
    at Kc (react-dom.min.fa9ca8c8.js:155)
    at ya (react-dom.min.fa9ca8c8.js:153)
    at Object.enqueueSetState (react-dom.min.fa9ca8c8.js:202)

Please see the following code that I am using in WordPress Gutenberg. I think I am missing something that I don't know. Any help will be appreciable.

const { __ } = wp.i18n; // Import __() from wp.i18n
import {
    SortableContainer,
    SortableElement,
    SortableHandle,
    arrayMove
} from 'react-sortable-hoc';
const { registerBlockType } = wp.blocks;

registerBlockType( 'cgb/block-tabbed-content', {
    title: __( 'tabbed-content - CGB Block' ), // Block title.
    icon: 'shield',
    category: 'common',
    keywords: 'tabbed-content',

  attributes : {
    items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6'],
  },

edit: function( props ) {

    const { attributes, setAttributes } = props;

    const SortableItem = SortableElement(({value}) => <li>{value}</li>);

    const SortableList = SortableContainer(({items}) => {
      return (
        <ul>
          {items.map((value, index) => (
            <SortableItem key={`item-${index}`} index={index} value={value}/>
          ))}
        </ul>
      );
    });

    const onSortEnd = ({oldIndex, newIndex}) => {
      setAttributes(({items}) => ({
        items: arrayMove(items, oldIndex, newIndex),
      }));
    };

    return (
        <div className={ props.className }>
            <SortableList items={attributes.items} onSortEnd={onSortEnd} />
        </div>
        );
    },
1

1 Answers

0
votes

You need to reference items property from the attributes object. See if this helps.

Update this.

{items.map((value, index) => (
     <SortableItem key={`item-${index}`} index={index} value={value}/>
))}

to this.

{attributes.items.map((value, index) => (
     <SortableItem key={`item-${index}`} index={index} value={value}/>
))}