Skip to content

Commit fad8236

Browse files
author
Rustem Mussabekov
authored
Fix issue when one of a children is undefined
Firstly huge thanks for such amazing module! In some edge cases (like in my own) there can be a `undefined` React child. Easy to reproduce: ```js <ReactSortable> {some_var ? <li>123</li> : undefined} <li>123</li> </ReactSortable> ``` In such case crash happen because code can't read property of undefined object. Just check changes that I made into `react-sortable.tsx` file you will understand what I'm trying to achieve. I also changed this line from `const item = list[index];` to `const item = list[index] || {};` , just to be sure that if list doesn't have particular index, whole code not fail. This two changes are made to prevent edge case crashes. In my own app I spend a lot of time to find out why sometime app is crashing. Please, please approve this pull request!
1 parent a104520 commit fad8236

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/react-sortable.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ Please read the updated README.md at https://github.com/SortableJS/react-sortabl
110110
const dataid = dataIdAttr || "data-id";
111111
/* eslint-disable-next-line */
112112
return Children.map(children as ReactElement<any>[], (child, index) => {
113-
const item = list[index];
113+
if (!child) return undefined
114+
115+
const item = list[index] || {};
114116
const { className: prevClassName } = child.props;
115117

116118
// @todo - handle the function if avalable. I don't think anyone will be doing this soon.

0 commit comments

Comments
 (0)