Keys and Loops
When using a for
loop, Melody is no longer able to avoid DOM mutations unless you specify a key
attribute. Supplying the key
attribute will allow Melody to recognise removed, moved and replaced elements and perform a minimal amount of changes.
The key
to a fast loop
key
to a fast loopWhen using a loop, Melody is no longer able to avoid DOM mutations unless you specify a key
attribute.
Supplying the key
attribute will allow Melody to recognise removed, moved, and replaced elements and perform a minimal amount of changes if necessary. Without the key
, it'd have to potentially mutate many more elements.
Thekey
attribute must never be filtered through theattrs
filter. Thekey
attribute is only used internally by Melody and its value does not even appear in the rendered HTML. Filtering it throughattrs
hides it from Melody, and it also causes it to be rendered as an attribute in the HTML (thus causing invalid HTML).
About keys
The key
attribute a unique identifier within a loop and is used by Melody to make the minimum changes possible within the loop, where possible.
The key
attribute must not be filtered through the attrs
filter.
The key
attribute is not visible on the rendered HTML.
Example
The use of key
here, on each item in a loop, helps Melody to decide which item to update if it needs to, so not all items would be re-rendered.
Last updated
Was this helpful?