首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在卸载的组件上找不到节点(react-sortable-hoc)

在卸载的组件上找不到节点(react-sortable-hoc)
EN

Stack Overflow用户
提问于 2021-08-16 10:24:30
回答 1查看 385关注 0票数 0

有一个小的gutenberg块,但我有错误。

在未装入的组件上找不到节点未定义不是对象(计算“”this.refscollection.indexOf“”)

当前的wp版本使用React 16.13.1

我该如何解决这个问题呢?谢谢!

代码语言:javascript
复制
    import {SortableContainer, SortableElement} from 'react-sortable-hoc';
    
    registerBlockType('ay/sortable', {
        title: __('Sortable'),        
        attributes: {
            items: {
                type: 'array',
                default: [
                    'Item 1',
                    'Item 2',
                    'Item 3',                    
                ],
            },
        },
    
        edit(props) {
            const {
                attributes: {items},
                setAttributes,
            } = props;
    
            const SortableItem = SortableElement(({value}) => <li>{value}</li>);
    
            const SortableList = SortableContainer(({items}) => {
                return (
                    <ul>
                        {items.map((value, index) => (
                            <SortableItem
                                key={`item-${value}`}
                                index={index}
                                value={value}
                            />
                        ))}
                    </ul>
                );
            });
    
            const onSortEnd = ({oldIndex, newIndex}) => {
                setAttributes(({items}) => ({
                    items: arrayMove(items, oldIndex, newIndex),
                }));
            };
    
            return (
                <div>
                    <SortableList items={items} onSortEnd={onSortEnd} />
                </div>
            );
        },
    
        save(props) {
            return null;
        },
    });
EN

回答 1

Stack Overflow用户

发布于 2021-08-18 01:16:55

在您提供的代码中,缺少两个关键导入才能使您的块工作,添加:

代码语言:javascript
复制
...
import { registerBlockType } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
...

还要检查您的package.json是否将react-sortable-hoc列为依赖项:

代码语言:javascript
复制
...
"devDependencies": {
    ...
    react-sortable-hoc": "^2.0.0"
},

在添加了缺少的导入并确保依赖项存在/安装之后,您的代码块应该会编译无误。我测试了您的代码,edit()函数的所有其他部分都没有问题,并且输出是一个可排序的列表。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68801203

复制
相关文章

相似问题

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