Basic Array Rendering
What is the most common method for displaying a list of items from an array in a user interface framework like React or Vue?
- A. Using a while loop directly in the template.
- B. Mapping over the array and generating elements for each item.
- C. Stringifying the array and displaying it as text.
- D. Using a for...in loop and accessing array indices.
- E. Appending array elements to the DOM one at a time.
Key Prop Importance
When rendering a list of items, what is the primary purpose of providing a 'key' prop to each rendered element, and why is it important to avoid using the array index as the 'key' when the list items are likely to change order?
- A. To style the elements differently based on their position in the list.
- B. To help the framework efficiently update and re-render the elements when the list changes.
- C. To provide a unique identifier for each element for SEO purposes.
- D. To prevent the browser from caching the list elements.
- E. To prevent the list from being re-rendered at all.
Rendering Complex Objects
Suppose you have an array of objects, each with properties like 'name' and 'price'. What is the best way to display the 'name' property of each object within a list item?
- A. By directly referencing the object within the list item tag.
- B. By accessing the 'name' property using bracket notation within the template or JSX.
- C. By creating a function that iterates through the array and concatenates the names.
- D. By accessing the 'name' property using dot notation within the template or JSX.
- E. By only mapping over the array and extracting the name value using a loop
Conditional Rendering in Lists
If you want to conditionally render certain list items based on a property of the item (e.g., only displaying items where 'isActive' is true), where is the appropriate place to include the conditional statement?
- A. Within the loop, before mapping over the array to filter results.
- B. Outside the loop, before calling the loop.
- C. Within the mapping function or rendering logic for each item.
- D. In the parent component, before passing the array to the child.
- E. By editing the original array.
Handling Empty Arrays
What is the recommended approach for displaying a message when the array passed to your list rendering function is empty, instead of displaying nothing at all?
- A. Throw an error to indicate that the array is invalid.
- B. Use a conditional statement to check if the array is empty and render a placeholder message.
- C. Set a default value for the array to an array with a single empty string.
- D. Let the mapping function handle the empty array, it won't do anything.
- E. Hide the list from the user.