question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pass parameters to onSortEnd function

See original GitHub issue

I’m trying to pass a parameters to onSortEnd function but always the data is empty for mine but the indexing work fine.

onSortEndCustom = ({oldIndex, newIndex}, data) => { console.log(data); }

I create my custom function it is working but seems call every time the list changes even if not change the sort, I want same concept as onSortEnd.

So any one know why it is not working to pass parameters or any best solutions?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
XeniaSiskakicommented, Jul 2, 2019

I don’t think the collection parameter is used for the use case that @Adel-J mentioned in his first comment. From the docs:

The collection the element is part of. This is useful if you have multiple groups of sortable elements within the same SortableContainer

which if I understand correctly means that the value will just be an identifier for the group and not some custom data.

@clauderic I’m facing the same problem. I want to pass some custom data and read them in the onSortEnd. Is that currently possible? To be more specific, each SortableElement in my SortableContainer has an ID which I need to know when handling the sort end.

2reactions
sercanovcommented, May 24, 2020

@XeniaSiskaki is right, when you pass collection attribute to SortableItem, sorting brokes. I also need to get item ID ( or any other data ) in onSortEnd function. The only way seems to work is adding custom attribute to SortableItem, and try to parse it in onSortEnd’s nodes property. Like this;

  1. Add custom field to SortableItem
const SortableList = SortableContainer(
  ({ ids = [], data = [], onClick, onEdit, selected, ...props }) => {
    if (data)
      return (
        <List component="nav">
          {ids.map((id, index) => (
            <SortableItem
              key={id}
              index={index}
              // collection={id} this brokes sorting 
              itemID={id} // Add custom field to pass data
              value={data[id]}
            />
          ))}
        </List>
      );
  }
);
  1. Parse itemID in onSortEnd
    const onSortEnd = ({ oldIndex, newIndex, collection, nodes, ...rest }) => {
      console.log(nodes[oldIndex].node.attributes.itemid.value);
    };
image

Conclusion; All this should be less painful

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - react-sortable-hoc -- maintain new sort order without ...
The issue is in the onSortEnd callback: onSortEnd(oldIndex, newIndex) { this.setState(({items}) => ({ items: arrayMove(items, oldIndex, ...
Read more >
How to use the react-sortable-hoc.SortableContainer function ...
SortableContainer function in react-sortable-hoc ... getPrototypeOf(ModalTabListWrapper)).apply(this, arguments)); } ... onSortEnd = this.
Read more >
Drag and Drop in React with 'react-sortable-hoc' - Medium
As you may have noticed, we are passing index as an argument to '.map.' This is because we have to assign each of...
Read more >
@alexcheuk/react-sortable-hoc - npm
Use this function to specify a custom container object (eg this is useful for integrating with certain 3rd party components such as FlexTable...
Read more >
How to Pass Parameters to Javascript Function? - YouTube
In this lecture, you will learn how to pass parameters to JavaScript function, how to create JavaScript Functions with one or more ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found