【问题标题】:Cannot read property 'indexOf' of undefined - react-sortable-hoc无法读取未定义的属性“indexOf”-react-sortable-hoc
【发布时间】:2020-01-17 17:49:43
【问题描述】:

我正在尝试获取以下结构。

https://imgur.com/NBRGlhM

三列,带有标题和列卡,只能在一列内移动。 this.state.item ['lists'] 移动到组件 SortableList。然后在items.map ((item, index) => 之后迭代并移动到组件SortableItem。然后在value.listItems之后迭代,想要在列中显示列的标题和卡片。我得到了错误:

无法读取未定义的属性“indexOf”

在这里演示:https://stackblitz.com/edit/react-jpszoq

import {SortableContainer, SortableElement} from 'react-sortable-hoc';
import arrayMove from 'array-move';


const SortableItem = SortableElement(({value}) => {
  return(
    value.listItems.map((it, index) => {
      <li>{it.title}</li>
    })
  )    
})

const SortableList = SortableContainer(({items}) => {
  console.log(items)
  return (

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

      }
    </ul>
  );
});

class App extends Component {
  constructor() {
    super();
    this.state = {
       item: {
        id: "abc123",
        name: "AAA",
        lists: [
          {
            id: "def456", 
            list_id: "654wer",
            title: 'List1',
            desc: "description",
            listItems: [
              {
                id: "ghj678", 
                title: "ListItems1",
                listItemsId: "88abf1"
              },
              {
                id: "poi098", 
                title: "ListItems2",
                listItemsId: "2a49f25"
              }
            ]   
          },
          {
            id: "1ef456", 
            list_id: "654wer",
            title: 'List 2',
            desc: "description",
            listItems: [
              {
                id: "1hj678", 
                title: "ListItems3",
                listItemsId: "18abf1"
              },
              {
                id: "1oi098", 
                title: "ListItems4",
                listItemsId: "1a49f25"
              }
            ]   
          },
          {
            id: "2ef456", 
            title: 'List 3',
            list_id: "254wer",
            desc: "description",
            listItems: [
              {
                id: "2hj678", 
                title: "ListItems5",
                listItemsId: "28abf1"
              },
              {
                id: "2oi098", 
                title: "ListItems6",
                listItemsId: "234a49f25"
              }
            ]   
          }
        ]
      }
    };
  }

  onSortEnd = ({oldIndex, newIndex}) => {
    this.setState(({lists}) => ({
      lists: arrayMove(lists, oldIndex, newIndex),
    }));
  };

   render() {
     return <SortableList items={this.state.item['lists']} onSortEnd={this.onSortEnd} />;
  }
}

【问题讨论】:

    标签: javascript reactjs react-sortable-hoc


    【解决方案1】:

    您在传递给 listItems.map 的函数中缺少 L14 上的 return。或者,您可以移除大括号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-14
      • 2016-08-26
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多