首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在WordPress Gutenberg中使用react-sortable-hoc

如何在WordPress Gutenberg中使用react-sortable-hoc
EN

Stack Overflow用户
提问于 2019-04-24 06:19:41
回答 1查看 297关注 0票数 0

Gutenberg仍然很新,但我仍然希望有人已经遇到了这个问题并找到了解决方案。

我已经使用create-guten-block创建了一个项目的样板,并创建了一个选项卡式内容块。我使用react-sortable-hoc和Gutenberg来交换项目列表中的项目。我尝试了几种解决方案,但都没有得到答案。我遇到的问题是我在控制台中得到这个错误,它不能读取未定义的映射。我认为未定义的是items对象。

代码语言:javascript
复制
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)

请参阅我在WordPress古腾堡中使用的以下代码。我想我遗漏了一些我不知道的东西。任何帮助都是值得欣赏的。

代码语言:javascript
复制
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>
        );
    },
EN

回答 1

Stack Overflow用户

发布于 2019-08-24 20:27:48

您需要从attributes对象引用items属性。看看这是否有帮助。

更新这个。

代码语言:javascript
复制
{items.map((value, index) => (
     <SortableItem key={`item-${index}`} index={index} value={value}/>
))}

对这个。

代码语言:javascript
复制
{attributes.items.map((value, index) => (
     <SortableItem key={`item-${index}`} index={index} value={value}/>
))}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55820153

复制
相关文章

相似问题

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