替代MapArray在删除物品时等待承诺的MapArray
我不想使用过渡组或任何多余的代码。我只希望以下API样式起作用。
1秒后的输出为AA 3 4 5 6
2秒后的输出变为3 4 5 6
import { render } from "solid-js/web";
import { For, createSignal, createMemo, mapArray } from "solid-js";
function make_position(_: number) {
let [removing, setRemoving] = createSignal()
return {
get value() {
return removing() ? 'a': _
},
async on_remove() {
// I want `mapArray` to call this function when an item is removed, and wait on the returned promise to actually remove the item.
setRemoving(true)
return new Promise(resolve => {
setTimeout(resolve, 1000);
})
}
}
}
function Counter() {
const [list1, setList1] = createSignal([1, 2, 3, 4, 5]);
const ls = createMemo(mapArray(list1, _ => make_position(_)))
setTimeout(() => {
setList1([3,4,5,6])
}, 1000)
return (<>
<For each={ls()}>{ item =>
<span>{item.value}</span>
}</For>
</>)
}
render(() => <Counter />, document.getElementById("app"));
I don't want to use transition-group or any superfluous code. I just want the below API style to work.
The output after 1 second is a a 3 4 5 6
The output after 2 second becomes 3 4 5 6
import { render } from "solid-js/web";
import { For, createSignal, createMemo, mapArray } from "solid-js";
function make_position(_: number) {
let [removing, setRemoving] = createSignal()
return {
get value() {
return removing() ? 'a': _
},
async on_remove() {
// I want `mapArray` to call this function when an item is removed, and wait on the returned promise to actually remove the item.
setRemoving(true)
return new Promise(resolve => {
setTimeout(resolve, 1000);
})
}
}
}
function Counter() {
const [list1, setList1] = createSignal([1, 2, 3, 4, 5]);
const ls = createMemo(mapArray(list1, _ => make_position(_)))
setTimeout(() => {
setList1([3,4,5,6])
}, 1000)
return (<>
<For each={ls()}>{ item =>
<span>{item.value}</span>
}</For>
</>)
}
render(() => <Counter />, document.getElementById("app"));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
跟踪信号的价值足以实现所需的目标。
已经使用
maparray
和createMeMo
内部,因此const ls = createMemo(maparray(list1,_ =&gt; make_position)(_ )))
是不必要的。您可以使用承诺来设置新值。如果您要通过网络请求接收新值,请使用资源。
Tracking the signal's value is enough to achieve what you want.
For
already uses bothmapArray
andcreateMemo
internally, soconst ls = createMemo(mapArray(list1, _ => make_position(_)))
is unnecessary.You can use a promise to set the new value. If you are going to receive the new value through network request, use a resource.