react basics coursera week 2 quiz answers

Knowledge check: Events and errors

1. When handling a click event in react, you should use the following attribute:

  • Onclick
  • onClick
  • OnClick
  • on-click

2. Inside a JSX element, you can assign a JSX expression to the onClick handler to handle a click in React.

  • False
  • True

3. You can place an opening and a closing parenthesis after the name of the event-handling function that you assign to the onClick attribute.

  • True
  • False

4. The try...catch syntax can be used in React in certain cases.

  • True
  • False

5. Choose the valid example of an onclick event handler.

  • <button onClick={handleClick}>Click me</button>
  • <button onclick={handleClick}>Click me</button>
  • <button on-click=”handleClick”>Click me</button>
  • <button onClick={handleClick()}>Click me</button>

Knowledge check: Dynamic events and how to handle them

6. What code should be added to the element button to make this code snippet valid?

function App() {

function handleClick() {
console.log("clicked")
}

return (
< div className="App">
< button >Click me< /button>
< /div>
);
}

  • onClick={handleClick}
  • onClick={handleClick()}
  • click=handleClick

7. Imagine that you have a variable named userLoggedIn and it’s set to the boolean of true. How would you complete the below clickHandler function declaration to toggle the value of the userLoggedIn boolean?

function clickHandler() {
}

  • userLoggedIn = true
  • userLoggedIn = !userLoggedIn
  • userLoggedIn = false

8. Is a click handler on its own enough to change the values of variables in your React apps?

  • No
  • Yes

Shuffle Q/A 1

9. What are the ways to write an event-handling function in React. Select all that apply.

  • Using a separate function expression
  • Using a separate function declaration
  • With an inline, anonymous ES6 function (an arrow function)
  • With an inline anonymous ES5 function

10. Choose the appropriate code on line 3 of the following component – to handle a click event.

function App() {

function () {
console.log("clicked")
}

return (
< div className="App">
< button onClick={handleClick}>Click me< /button>
< /div>
);
}

  • function handleClick {
  • function handleClick() {
  • function HandleClick() {

Leave a Reply