javascriptreactjsreact-sortable-hoc

Cannot read property 'map' of undefined when creating drag and drop cards


I use the library: https://github.com/clauderic/react-sortable-hoc

I'm trying to achieve this effect: https://i.sstatic.net/WGQfT.jpg

Demo here: https://stackblitz.com/edit/react-r7hzxk

I move this.state.item ['lists'] to the component SortableList. In the component SortableList wants to iterate items.listItems.map but I have error:

Cannot read property 'map' of undefined

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


const SortableItem = SortableElement(({value}) => <li>{value}</li>);

const SortableList = SortableContainer(({items}) => {
  return (
    <ul>
      {items.listItems.map((value, index) => (
        <SortableItem key={`item-${value.id}`} index={index} value={value.id} />
      ))}
    </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} />;
  }
}

Solution

  • You need to iterate through items array first and don't forget to also return the result:

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