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.

Using object array instead of a string array for id?

See original GitHub issue

My sortable item has several props exposed such as title, category and image.

So instead of

[items, setItems] = useState(['1', '2', '3']);

My sortable item looks like:

[items, setItems] = useState([
{id: '1', title: 'x', category: 'y'},
{id: '2', title: 'z', category: 'w'}
...])

I am not sure how I can make this work.

Any help would be appreciated!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
vbylencommented, Jan 26, 2021

Awesome.

Using ‘sortedItemIds’, how would you reconfigure the handleDragEnd function?


function handleDragEnd(event) {
    const {active, over} = event;
    
    if (active.id !== over.id) {
      setItems((items) => {
        const oldIndex = items.indexOf(active.id);
        const newIndex = items.indexOf(over.id);
        
        return arrayMove(items, oldIndex, newIndex);
      });
    }
  }

Replacing ‘items’ by ‘items.id’ does not seem to work.

fixed it with following code:

const oldIndex = items.map((item) => item.id).indexOf(active.id);
            const newIndex = items.map((item) => item.id).indexOf(over.id);
2reactions
claudericcommented, Jan 25, 2021

As mentioned by @TheUrbina20, the solution is to map the items to their corresponding unique identifiers.

You may want to memoize to avoid needless re-renders of the SortableContext:

const [items, setItems] = useState([
  {id: '1', title: 'x', category: 'y'},
  {id: '2', title: 'z', category: 'w'},
  ...
])
const sortedItemIds = useMemo(() => items.map((item) => item.id), [items]);

<SortableContext
  items={sortedItemIds}
>
  {...}
</SortableContext>
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get an array of IDs as strings instead of object with ...
I want the result to be an array. There reason why I want it to be a list is that I want to...
Read more >
Data Structures: Objects and Arrays - Eloquent JavaScript
The elements in an array are stored as the array's properties, using numbers as ... You give it an object, and it returns...
Read more >
Array.prototype.filter() - JavaScript - MDN Web Docs
The filter() method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given...
Read more >
5. Working with Arrays and Loops - JavaScript Cookbook [Book]
An array is an ordered collection of elements. In JavaScript, an array can be created using formal object notation, or it can be...
Read more >
How to print object by id in an array of objects in JavaScript?
How to pretty print JSON string in JavaScript ? ... JavaScript | How to add an element to a JSON object? ... How...
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