javascript - Svelte,只重新渲染列表中的一个项目,而不是整个列表
问题描述
无论是svelte-apollo
(https://github.com/timhall/svelte-apollo/issues/19)和@urql-svelte
(https://github.com/FormidableLabs/urql/issues/704)我都会重新渲染整个列表只是一个元素重新渲染。
复制步骤:
转到 REPL:https ://codesandbox.io/s/urql-svelte-list-rerendering-uwsvn
点击一个按钮
每个按钮重新渲染;所以整个列表重新渲染自己
我希望它只重新呈现我单击的按钮
如果我在这里阅读:https://svelte.dev/tutorial/svelte-options,我可以通过以下方式理解:
immutable={true} - 你从不使用可变数据,因此编译器可以进行简单的引用相等检查以确定值是否已更改
immutable={false} — 默认值。Svelte 对于可变对象是否发生变化会更加保守
如果您看到示例,他们正在更改todos
数组,就像我们也在做的那样urql
:
let todos = [
{ id: 1, done: true, text: 'wash the car' },
{ id: 2, done: false, text: 'take the dog for a walk' },
{ id: 3, done: false, text: 'mow the lawn' }
];
function toggle(toggled) {
todos = todos.map(todo => {
if (todo === toggled) {
// return a new object
return {
id: todo.id,
text: todo.text,
done: !todo.done
};
}
// return the same object
return todo;
});
}
为什么它不适用于我的 REPLtodos
示例?
最后是todos
一个新数组吗?
解决方案
推荐阅读
- java - 正确检查用户是否登录。 Android
- python - 无法为同一服务器上的多个 mod_wsgi 应用导入 Pandas
- date - 时间和日期标准?
- html - 使用 CSS position-absolute 放置元素
- bash - BASH 测试文件名是否以 .dylib 结尾
- cypress - 寻找一种方法,您可以在 it 块之外对我的所有自定义命令使用赛普拉斯夹具
- r - Write.csv 写入 unix 可执行文件 mac
- java - 从 db2 数据库 Spring Boot 在内存数据库中填充 h2
- node.js - 通过 NestJs 框架通过 Passport Facebook 登录
- python - 如何在虚拟环境中(在 Windows 中)升级 python 版本?