Actually, the LinkControl
index is already there in your code.. it’s in the items.map( ( item, index ) )
— yes, that index
. So just use it in your onChange
callback.
And presuming that your edit function starts like ( { attributes, setAttributes } ) => { ...
, i.e. the setAttributes
is defined, you can try the following in place of what you currently have:
items.map( ( item, index ) => (
<LinkControl
value={ { ...item } }
onChange={ ( value ) => {
// do not change the existing 'items' array; clone it instead
const newItems = [ ...items ];
// then update the one being edited
// the 'index' below is the one passed to .map() above
newItems[ index ] = { ...newItems[ index ], ...value };
// then update the block attributes
setAttributes( { items: newItems } );
console.log( value, newItems, items ); // for testing/debugging
} }
/>
) )