S
SolidJS10mo ago
KokoNeot

remove element from store array createStore([])

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
}
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
}
1 Reply
bigmistqke
bigmistqke10mo ago
something isn't working? I tend to do setStore(store => ...manipulate store here...) otherwise you can create infinite loops:
createEffect(() => {
store.splice(...)
setStore(store)
})
createEffect(() => {
store.splice(...)
setStore(store)
})
will run forever.
const newCards = cards
newCards.splice(cardIdx, 1)
const newCards = cards
newCards.splice(cardIdx, 1)
this might not do what u expect it to do. you are mutating cards here too. u could look into produce : https://docs.solidjs.com/reference/stores/store-utilities
Want results from more Discord servers?
Add your server