#remove element from store array createStore([])

4 messages · Page 1 of 1 (latest)

atomic whale
#

hi, i need to move element in store to the front of the array or delete it and add it to the front. i used signals, now switching to store.


            const rect = board.getBoundingClientRect()
            const card = cards[cardIdx]
            const newCards = cards
            newCards.splice(cardIdx, 1)
            setCards([{...card, position: { x: e.clientX - offsetX - rect.left, y: e.clientY - offsetY - rect.top}}, ...newCards])
//this is how the card object looks like, the store is array of those objects

const card = {
            title: "Card 4",
            body: "This is the body of the second card",
            color: "green",
            position: { x: 10, y: 50 },
            velocity: { x: 0, y: 0 },
            pins: [],
            div: undefined,
            id: 3
        }
dense horizon
#

something isn't working?

#

I tend to do setStore(store => ...manipulate store here...) otherwise you can create infinite loops:

createEffect(() => {
   store.splice(...)
   setStore(store)
})
```  will run forever.
#
const newCards = cards
newCards.splice(cardIdx, 1)
``` this might not do what u expect it to do. you are mutating `cards` here too.