I am trying to display a list where dragged item is on top.
I sorted the list thats nested inside createMemo
mapArray
.
It works in my case, but I couldn't reproduce it with this example.
#1 Problem, why doesnt this example sort and display the dragged items on top. Also createEffect console.log m_atoms_sorted
doesn't retrigger even though it touches the .dragging
signal of every atom.
#2 Problem, in my case it sorts, but the problem occurs when I try to remove an item from the original array via setList1
. In that case list1
correctly updates, but it display's the old items and removes the wrong item.
If I remove the sort the problem dissapears.
import { render } from "solid-js/web";
import { For, createSignal, createMemo, mapArray, createEffect } from "solid-js";
const make_atom = (n) => {
let [_dragging, _setDragging] = createSignal(false)
return {
n,
get dragging() { return _dragging() },
set dragging(v: boolean) { _setDragging(v) }
}
}
function Counter() {
const [list1, setList1] = createSignal([1, 2, 3, 4, 5]);
const m_atoms = createMemo(mapArray(list1, make_atom))
const m_atoms_sorted = createMemo(() => {
return m_atoms()
.sort((a, b) => a.dragging ? (b.dragging ? 0 : 1) : (b.dragging ? -1 : 0))
})
createEffect(() => {
console.log(m_atoms().map(_ => _.dragging).join(''))
})
createEffect(() => {
console.log(m_atoms_sorted())
})
return (<>
<For each={m_atoms_sorted()}>{ item =>
<span onClick={_ => item.dragging = !item.dragging}>|{item.n}{item.dragging ? 'drag':'nodrag'}|</span>
}</For>
</>)
}
render(() => <Counter />, document.getElementById("app"));
Edit
Ok, If I remove the createMemo for m_atoms_sorted
, it correctly puts dragging items on top. So my problem is #2.