Nuqs array toggle
How do I toggle value on nuqs array? Is this best way or is there other ways of doing it?
1 Reply
Yes, pretty much the same way you would in a useState. If you want to keep the order and toggle in-place (but still return a new array), you could use the newly available .with method on arrays:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/with
Look for the index first, then if it's not -1 call that (otherwise return the original to avoid a re-render).
MDN Web Docs
Array.prototype.with() - JavaScript | MDN
The with() method of Array instances is the copying version of using the bracket notation to change the value of a given index. It returns a new array with the element at the given index replaced with the given value.