2017-08-25 21 views
1

我想從道具從一個組件傳遞到另一個。但SortableSectionList中的index屬性似乎並未傳遞給SortableSection。見下面的控制檯輸出。組件沒有傳遞道具到同一文件中的其他組件時調用爲const

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 
+0

嗨,render()方法裏面似乎有太多東西。我建議你將它們分成組件方法或甚至是新的獨立組件。 – nbkhope

+0

謝謝,我會嘗試,看看它是如何去並相應地更新問題。 –

+0

代碼片段中沒有任何內容跳出錯誤。你還在面對這個問題嗎? – enjoylife

回答

1

似乎react-sortable-hoc本身使用index property。所以,如果你想使用它,你最好添加另一個屬性,如sortIndex或類似的,並通過它。

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

他們在文檔中也有explanation and example

+0

是的,謝謝!欣賞示例鏈接。 –

相關問題