advanced react coursera week 2 quiz answers

Knowledge check: Getting started with hooks

1. Imagine you have to log into the console a state variable, whenever the variable gets updated. What's the best place to perform such operation in a React component?

  • Before the return statement of the component
  • the useEffect hook

2. The useEffect hook accepts...

  • a callback function and an object
  • a callback function and an array
  • two callback functions

3. What is a pure React component?

  • A component that doesn’t have any side effects
  • A component that has at least one side effect

4. What is the name of the second argument of the useEffect() call?

  • the dependency array
  • the callback function
  • the destructured object
  • the dependencies object

5. This code is incomplete:

React.useEffect(()=> {
console.log('The value of the toggle variable is',toggle)
}, [])

You need to update the dependecies array so that the useEffect hook is invoked whenever the toggle variable updates. Choose the correct solution from the choices below.

  • The dependencies array should receive the toggle variable as its single member.
  • The dependencies array should be removed.
  • The dependencies array should be updated to: [{toggle}].
  • The dependencies array should be replaced with: {toggle}.

Knowledge check: Rules of Hooks and Fetching Data with Hooks

6. True or false: You should not call hooks inside loops.

  • True
  • False

7. True or false: You should call hooks inside if statements.

  • True
  • False

8. True or false: You should call hooks inside nested functions.

  • True
  • False

Shuffle Q/A 1

9. You are allowed to:

  • only call a single effect hook inside a component.
  • only call a single state hook inside a component
  • call multiple state hooks and effect hooks inside a component

10. True or false: You don't have to always make multiple hook calls in the same sequence.

  • True
  • False

Leave a Reply