Uncaught TypeError: Cannot read property 'top' of undefined(…)
See original GitHub issueInitally Dragging seems fine, i am re sorting my array and re setting the state on onSortEnd.
After 1-2 drags, I am able to drag single element but all other elements in list is stuck.Other Elements does not scroll.
onSortEnd: function (newProps) { var oldGroups = this.state.groups; oldGroups.move(newProps.oldIndex, newProps.newIndex); this.setState({groups: oldGroups}); },
Issue Analytics
- State:
- Created 7 years ago
- Comments:16 (3 by maintainers)
Top Results From Across the Web
Uncaught TypeError: Cannot read property 'top' of undefined
This malfunction occurs because your href is set to # inside of your <a> tag. Basically, your app is trying to find href...
Read more >Uncaught TypeError: Cannot read property 'top' of undefined
'Undefined' is the property of the global object. This error occurs in Chrome Browser when you read a property or call a method...
Read more >TypeError: Cannot read property 'top' of Undefined in JS
The "Cannot read property 'top' of undefined" error occurs when trying to access the top property on an undefined value. To solve the...
Read more >Uncaught TypeError: Cannot read property 'top' of undefined
I have use one page scroll but not working in live. custom.js:73 Uncaught TypeError: Cannot read property 'top' of undefined at HTMLAnchorElement.
Read more >Uncaught TypeError Cannot read property top of undefined
Here this line of code: var contentNav = $('.content-nav').offset().top; is firing an error: "Uncaught TypeError: Cannot read property 'top' ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I still haven’t reproduced it in JSFiddle - but I think I’ve found the source of the bug and the solution, in my project at least.
The issue was that components wrapped by
SortableElement
were being re-rendered but the wrappingSortableElement
component was not. This meant that thecomponentWillUnmount
event onSortableElement
was not firing so the refs in the manager class were not being updated, resulting in the refs pointing at DOM nodes that had been removed by the re-render.The fix was to set the
key
prop of theSortableElement
component to a string dependent on the information displayed within the wrapped component. This means that if the data changes such that the wrapped component needs to re-render, the wrappingSortableElement
will have it’s key changed causing it to re-render as well which will trigger the lifecycle events that will keep the manager class refs in sync.So we’ve got a wrapped SortableElement:
And an array containing the data for each card which we want to map over to generate the list. React will complain if the sibling items in the list do not have a unique key set - the solution I usually use for this is to use the array index to build the key:
However, that doesn’t work in this case. Sometimes the array index will stay the same, but the component needs to be re-rendered for react-sortable-hoc to work. If the data prop changes the wrapped component is re-rendered, but the wrapping component is not!
That’s what causes the weird behavior, all of the components whose index changed work fine - the ones where the index stayed the same no longer work. You can check out this behavior in React-Devtools.
The solution I’ve been using is to make it so that the key is dependent on the data in
card
. This will force react to re-render the wrapping component whenever the data changes, rather than when it’s index changes:I hope that helps 😃