advanced react coursera week 1 quiz answers

Knowledge check: Rendering Lists in React

1. Imagine you have an array with one object that represents a dessert. You would like to apply some transformation to the item to output a different structure using the map function as per the code below. What would be the value of the newDesserts variable?

const desserts = [
{
title: 'Chocolate Cake',
description: 'Chocolate cake is a cake flavored with melted chocolate',
calories: 500,
}
];

const newDesserts = desserts.map((dessert) => {
return {
title: dessert.title.toUpperCase(),
...dessert,
kCal: dessert.calories / 1000,
};
});

  • [
    {
    title: ‘CHOCOLATE CAKE’,
    description: ‘Chocolate cake is a cake flavored with melted chocolate’,
    kCal: 0.5,
    }
    ]
  • [
    {
    title: ‘Chocolate Cake’,
    description: ‘Chocolate cake is a cake flavored with melted chocolate’,
    calories: 500,
    kCal: 0.5,
    }
    ]
  • [
    {
    title: ‘CHOCOLATE CAKE’,
    description: ‘Chocolate cake is a cake flavored with melted chocolate’,
    calories: 500,
    kCal: 0.5,
    }
    ]

2. How do you access dynamic data inside the JSX from the render function?

  • Using local state in the component.
  • Using component props.
  • Wrapping the variable in question with curly braces.

3. What could be a potential problem of using a randomiser function that generates an integer number from 0 to 10 as a key for your list items, having a list of only eight items? Select all that apply

  • The randomiser function does not entirely guarantee that the keys it generates will be different per item and a collision could happen, having two items with the same integer as keys.
  • There is no persistence of the keys generated since the moment the component re-renders the keys will vary and that could cause unexpected UI changes.
  • The randomiser function is a potential performance bottleneck since it has to run every re-render and it’s an unnecessary computation.

4. The todos array contains a list of todo objects, where each object has an id property that is unique. Which of the following code snippets will throw a React warning when opening up the browser console? Select all that apply

  • {todos.map((todo, index) => (
    <ToDo id={todo.id} />
    ))}
  • {todos.map((todo, index) => (
    <ToDo key={index} id={todo.id} />
    ))}
  • {todos.map((todo, index) => (
    <ToDo key={todo.id} id={todo.id} />
    ))}
  • {todos.map((todo, index) => (
    <ToDo key=”myKey” id={todo.id} />
    ))}

5. What are the potential problems of using indexes as keys?

  • An index is not guaranteed to be unique.
  • If the order of items may change, that can negatively impact performance and may cause issues with component state.
  • The index is not persisted and will change the moment the component re-renders.

Knowledge check: Forms in React

6. What of the next input types doesn’t have a controlled version when they are used in React?

  • <textarea />
  • <input type=”file” />
  • <input type=”text” />

7. What are some of the features of controlled components? Select all that apply

  • Enforcing a specific input format.
  • Validating all values in the client side when a submission occurs in the form, before calling the server endpoint.
  • Conditionally disabling the submit button.

8. How do you get the value of an input when its state is handled by the DOM (Uncontrolled)? Select all that apply.

  • Using local state and initializing it to an empty string. Then, reading the input value from the event object when the submission happens and finally setting the local state with that value.
  • Using a ref via useRef hook, assigning it to the input and then reading the input value when the submission happens via ref.current.value.
  • Using a combination of useEffect and useRef hooks, where a ref is used on the uncontrolled input and then its value can be read on useEffect after a re-render cycle happens.

Shuffle Q/A 1

9. What happens when you click on the submit button in the below code snippet?

< form onSubmit={() => alert("Submitting")}>
< input type="text" value={text} onChange={e =>
setText(e.target.value)} / >
< input type="button" value="Submit" / >
< /form>

  • The onSubmit callback is executed and an alert is shown with the text “Submitting”.
  • An error is thrown.
  • Nothing happens when the button is clicked.

10. What is missing in the below code for the select component to work properly?

< select onChange={handleChange}>
< option value="grapefruit">Grapefruit< /option>
< option value="lime">Lime< /option>
< option value="coconut">Coconut< /option>
< option value="mango">Mango< /option>
< /select>

  • Each option tag should be accompanied by a label tag.
  • Each option tag should have an onChange handler.
  • The select tag is missing a value prop.

Leave a Reply