调用const时组件未将props传递给同一文件中的其他组件

大卫·耶瑟(David Yeiser)

我正在尝试将道具从一个组件传递到另一个组件。中的index属性SortableSectionList似乎并未传递给SortableSection请参阅下面的控制台输出。

Index in SortableSectionList: 0
Index in SortableSectionList: 1
(2) Index in SortableSection: undefined

其他属性,如menuSection获得通过就好了。请参阅下面的完整代码。任何帮助表示赞赏,谢谢!

import React from 'react'
import MenuSection from './MenuSection'
import { SortableContainer, SortableElement } from 'react-sortable-hoc'

class MenuSections extends React.Component {
  render() {
    const menuSections = this.props.menuSections

    const SortableSectionList = SortableContainer(({ menuSections, onSectionSortEnd }) => (
      <div>
        {menuSections.map(function(menuSection, index) {
          console.log('Index in SortableSectionList: ' + index)
          return (
            <SortableSection
              collection="section"
              key={`item-${menuSection.id}`}
              menuSection={menuSection}
              index={index}
              menuItems={menuSection.menuItems}
              onSortEnd={onSectionSortEnd}
            />
          )
        })}
      </div>
    ))

    const SortableSection = SortableElement(({ menuSection, index, menuItems, onSortEnd }) => {
      console.log('Index in SortableSection: '+index)

      return (
        <MenuSection
          key={menuSection.id}
          menuSection={menuSection}
          index={index}
          menuItems={menuItems}
          onSortEnd={onSortEnd}
        />
      )
    })

    return (
      <div>
        <SortableSectionList
          menuSections={this.props.menuSections}
          lockAxis="y"
          lockToContainerEdges
          onSortEnd={this.props.onSortEnd}
          onSectionSortEnd={this.props.onSectionSortEnd}
        />
      </div>
    )
  }
}

export default MenuSections
普罗斯特

似乎自己react-sortable-hoc使用index属性因此,如果您还想使用它,则最好添加另一个类似sortIndex或类似的属性并将其传递。

return (
  <SortableSection
    index={index}
    sortIndex={index}
    ...

他们在文档中也有一个解释和示例

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Angular 2中使用同一文件中的组件?

文件名来自同一文件夹中的其他文件

图像必须与组件放置在同一文件夹中吗?

使用 TS 自动导出同一文件夹中的所有组件

从同一文件系统中的其他存储库导入Go模块

PyQT 从同一文件中的其他类(Qthread)访问 UI 元素

什么是从React中从同一文件导入多个组件的正确方法,而不是导入每个组件

调用同一文件中的测试功能

从同一文件中调用PHP函数

在特定文件夹中运行文件时出错,而同一文件在其他文件夹中运行平稳

在同一文件系统层次结构中的应用程序之间共享催化剂组件

如何创建同一文件js的两个组件?

如何使用崇高的文字或Visual Studio代码在同一文件夹中打开其他标签?

在同一文件外传递和调用函数

当在同一文件中定义其调用函数时,如何在C中模拟函数?

是否有一个文件系统,当不同的用户访问同一文件路径时,该文件路径会转移到其他文件?

React:从同一文件调用异步方法

根据同一文件相应行中的另一个字段在UNIX文件中添加其他文件

读写同一文件时的Perl性能

对同一文件进行多次ajax调用以在同一文件中执行不同的代码?

茉莉花-监视在同一文件中调用的函数

备份同一文件夹中的文件时的Postfix日期时间

在同一组件中从组件调用方法

当一个文件中的值用作另一文件的参数时,如何将多个JSON文件传递给Onservable数组?

在同一文件中声明委托协议

将值存储在同一文件中

从同一文件中绘制多个系列

在PHP中多次包含同一文件

如何复制 csv 文件内容并在同一文件中使用其他数组重写?