質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
ドラッグ&ドロップ

コンピューターのGUIにおいて、バーチャルなものを「つかむ」ことによって選択し、別の場所や他のバーチャルなものの上に動かす行為、またはその行為に対応していることを指す。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Q&A

0回答

959閲覧

TypeError: Cannot read property 'index' of undefined

aiai8976

総合スコア112

ドラッグ&ドロップ

コンピューターのGUIにおいて、バーチャルなものを「つかむ」ことによって選択し、別の場所や他のバーチャルなものの上に動かす行為、またはその行為に対応していることを指す。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

0グッド

0クリップ

投稿2020/11/19 07:59

前提・実現したいこと

https://github.com/clauderic/react-sortable-hoc/tree/master/src/.stories/grouping-items
ここを見ながらReactでの複数要素のドラッグ&ドロップをしたいと考えています。
しかしながら、以下のようなエラーが出て困っています。
sortableinfoがundefinedになっているのでこのようなエラーが発生しています。
この場合はどうすれば良いのでしょうか。
indexを得たいだけなので他の方法もありそうです。
わかる方がいましたら、コメントお願いします。

発生している問題・エラーメッセージ

TypeError: Cannot read property 'index' of undefined
203 | const item = items[event.target.sortableInfo.index];

該当のソースコード

import React from 'react'; import arrayMove from 'array-move'; import {generateItems} from './utils'; import SortableList from './List'; class GroupedItems extends React.Component { state = { selectedItems: [], items: generateItems(50), }; render() { const {items, isSorting, selectedItems, sortingItemKey} = this.state; return ( <SortableList items={items.filter(this.filterItems)} isSorting={isSorting} sortingItemKey={sortingItemKey} selectedItems={selectedItems} onItemSelect={this.handleItemSelect} shouldCancelStart={this.handleShouldCancelStart} updateBeforeSortStart={this.handleUpdateBeforeSortStart} onSortStart={this.handleSortStart} onSortEnd={this.handleSortEnd} distance={3} /> ); } filterItems = (value) => { const {selectedItems, sortingItemKey, isSorting} = this.state; // Do not hide the ghost of the element currently being sorted if (sortingItemKey === value) { return true; } // Hide the other items that are selected if (isSorting && selectedItems.includes(value)) { return false; } // Do not hide any other items return true; }; handleUpdateBeforeSortStart = ({index}) => { return new Promise((resolve) => this.setState( ({items}) => ({ sortingItemKey: items[index], isSorting: true, }), resolve, ), ); }; handleSortStart() { document.body.style.cursor = 'grabbing'; } handleSortEnd = ({oldIndex, newIndex}) => { const {selectedItems} = this.state; let newItems; if (selectedItems.length) { const items = this.state.items.filter( (value) => !selectedItems.includes(value), ); newItems = [ ...items.slice(0, newIndex), ...selectedItems, ...items.slice(newIndex, items.length), ]; } else { newItems = arrayMove(this.state.items, oldIndex, newIndex); } this.setState({ items: newItems, isSorting: false, sortingItemKey: null, selectedItems: [], }); document.body.style.cursor = ''; }; handleItemSelect = (item) => { this.setState(({selectedItems}) => { if (selectedItems.includes(item)) { return { selectedItems: selectedItems.filter((value) => value !== item), }; } return { selectedItems: [...selectedItems, item], }; }); }; handleShouldCancelStart = (event) => { const {items, selectedItems} = this.state; const item = items[event.target.sortableInfo.index]; // Never cancel start if there are no selected items if (!selectedItems.length) { return false; } // If there are selected items, we want to cancel sorting // from starting when dragging elements that are not selected return !selectedItems.includes(item); }; } export default GroupedItems;

試したこと

currentTarget

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

aiai8976

2020/11/20 02:33

これを参考にreact-sortable-hocを使って自分の環境で一度に複数要素のドラッグ&ドロップによるソートを実装したいということです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問