Guide: List Rendering Flashcards

1
Q

What is the syntax of v-for?

A

item in items where items is the source data array, and item is an alias for the array element being iterated on

You can also use item of items

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can access the index in a v-for?

A

v-for="(item, index) in items"

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the three syntaxes for iterating over an object?

A
  • Iterating through the values of an object’s properties
    • v-for="value in object"
  • Iterating through the values and keys of an object’s properties
    • v-for="(value, key) in object"
  • Iterating through the values, keys, and the index
    • v-for="(value, key, index) in object"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How is order determined when looping through objects?

A

It’s determined using the builtin Object.keys(), which is not guaranteed to be consisten across JS engines

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the default strategy Vue uses when updating a list rendered with v-for, and when is it suitable?

A

“in-place patch”

Only suitable when…

  1. Your list render output does not rely on child component state
  2. Your list render output does not rely on temporary DOM state (such as an input filter box)
  3. The content is simple
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you enable Vue to track each node’s identity, and what will this allow Vue to do?

A

Use a unique value in the key attribute.

Allows Vue to track each node’s identity, and thus reuse and reorder existing elements

`

`

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does Vue do to observed arrays? Give detail.

A

Vue wraps observed arrays’ mutation methods so that they’ll trigger view updates

The wrapped methods are:

  • push()
  • pop()
  • shift()
  • unshift()
  • splice()
  • sort()
  • reverse()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Describe how replacing an array works with non-mutation methods.

A

Some methods, like filter(), do not mutate the array, and return a new one.

If you assign the filtered array to the existing one, it will be a very efficient operation since Vue maximizes DOM element reuse when replacing an array with another array containing overlapping objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the detection caveats in relation to arrays? And how we do overcome them?

A
  1. Vue cannot detect directly setting an item with an index
    • vm.items[idx] = newValue
    • Two options to overcome this:
      1. vm.$set(vm.items, idx, newValue)
      2. vm.items.splice(idx, 1, newValue)
  2. Vue cannot detect when you modify the length of the array
    • vm.items.length = newLength
    • Use this to deal with this caveat
      • vm.items.splice(newLength)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the object change detection caveats?

A
  1. Vue cannot detect object property addition or deletion
    • Can overcome this by using vm.$set to add reactive properties
      • `vm.$set(vm.userProfile, ‘age’, 32)
  2. To assign multiple new properties to an existing object:
    • Create a new object with properties from both, using Object.assign() or _.extend() and then assign to fresh object
      • vm.userProfile = Object.assign({}, vm.userProfile, {...})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How should we handle displaying filtered or sorted results of an array?

A

Use a computed property that returns the filtered or sorted array.

In situations where computed properties are not feasible, like in a v-for loop, use a method.

  • Understand that using any instance data in the computed property will not trigger the property to update in the view because it’s not actually a bound property. The method was simply invoked for each render.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can we iterate over a range in the template?

A

Use v-for with an integer. It will repeat the template that many times.

{{ n }}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you render a block of multiple elements a certain number of times?

A

Use v-for on an encapsulating template tag

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Describe v-for and v-if on the same node.

A

When existing on the same node, v-for has a higher priority than v-if, thus, v-if will be run on each iteration of the loop separately.

If you conditionally want to skip the loop, place v-if on a wrapper element, or template tag

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Describe using v-for with a component.

A
  • v-for can be used directly on a custom component, like any other element
  • the key attribute is required for custom components
  • Using v-for on a custom component does not automatically pass any datat to the component.
    • You must still explicity pass your props in
      *
How well did you know this?
1
Not at all
2
3
4
5
Perfectly