D03
Warning
Data and assets
D03 — A persisted value every instance shares
A persist in a component placed more than once, or in a loop, is one stored value for all of them. key: gives each its own.
What the compiler said
warning[D03]: `persist open` is in `Panel`, which is placed more than once, so every one of them reads and writes one stored value --> src/App.wf:2:5 | 2 | persist open = false | ^^^^^^^ = help: Give each its own: `persist open = false { key: id }`, with `id` a prop that tells them apart = docs: https://webfluent.monzeromer.dev/errors/D03
The fix
Broken
component Panel(_ title: String) {
persist open = false
Card { Text(title) }
}
page P(path: "/", title: "T", description: "D") {
Heading("Panels").h1
Panel("One")
Panel("Two")
}
Fixed
component Panel(_ title: String) {
persist open = false { key: title }
Card { Text(title) if open { Text("Open") } }
}
page P(path: "/", title: "T", description: "D") {
Heading("Panels").h1
Panel("One")
Panel("Two")
}
Give each instance its own key: persist open = false { key: title
}, with a prop that tells them apart.
Each example is a whole program: it is all of src/App.wf in a project with no other .wf file (trying an example).
Why this rule exists
Warning.
lints may lower it:
"lints": { "D03": "off" }
// wf-allow(D03)