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
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() {
Knowledge check: Data flow
11. Usually, a React app consists of many components, organized as a component tree.
- True
- False
12. Uni-directional data flow is...
- The term that describes the one-way flow of components in a React app
- The term that describes the one-way flow of data in a React app.
- The term that describes the one-way flow of DOM updates in a React app
13. A component can, at any given time_______. Select all that apply.
- Receive data as props
- Pass data as props
- Pass data as props and receive data as props at the same time
14. You can only pass a single prop to a component.
- True
- False
15. The props parameter is:
- An array
- A string
- An object
- A boolean
16. Consider the following piece of code:
function MyMenu() {
return (
< div>
< Appetizers />
< /div>
)
}
Which element of this code represents a child component?
- <div>
- <Appetizers />
- MyMenu()
- return
Knowledge Check: State the concept
17. In React, can state be considered data?
- Yes
- No
18. In React, can props be considered data?
- Yes
- No
19. Choose the correct statement.
- The props object represents data that is external to a component, and state represents data that is internal to a component.
- The props object represents data that is internal to a component, and state represents data that is external to a component.
20. What does the useState hook do?
- It allows a component to receive state from its parent.
- It allows a component to have its own state.
21. Based on the code below, is the userName variable external or internal data of the DisplayUser component?
function DisplayUser(props) {
return (
< h1>{props.userName}< /h1>
);
}
- The userName value is data that is external to the DisplayUser component
- The userName value is data that is internal to the DisplayUser component
Knowledge check: Passing state
22. What is the Context API?
- A way to change the execution context of a function in JavaScript.
- An alternative way to working with state in React.
23. When working with useState to keep state in a variable, you should not use array destructuring.
- True
- False
24. If a state variable is destructured from useState, and set to variable name of user, the name of the function to update the user state variable should be...
- userSetter
- useUser
- useState
- setUser
25. What does the concept of โlifting up stateโ entail?
- It involves moving the state from the parent component to the child component.
- It involves moving the state from the child component to the parent component.
26. What is a negative result of lifting up state in a React app?
- Prop drilling.
- It can significantly increase the number of components that you need to create.
- There are no negatives from lifting up state in React.
Knowledge check: State or stateless
27. What is a stateless component?
- A component that doesnโt track its parentโs state.
- A component that doesnโt track its own state.
28. A stateful component must have a props object.
- False
- True
29. To turn a stateless component into a stateful component, you must pass it a props object.
- True
- False
30. The process of lifting up state can lead to:
Select all that apply.
- A stateful child component controlling the state of a stateless parent component.
- A stateless component becoming a stateful component.
- A stateful child component controlling the state of a stateful parent component.
- A stateful component becoming a stateless component.
31. A prop doesn't have to always pass state.
- True
- False
Module quiz: Data and state
32. In React, data flows in one way: from a parent component to a child component.
- True
- False
33. Why is one-way data flow important in React?
- It ensures that the data is flowing from top to bottom in the component hierarchy.
- It ensures that the data is flowing from bottom to top in the component hierarchy.
34. True or false? State data is the data inside a component that a component can mutate.
- True
- False
35. What is prop drilling?
- Prop drilling is a situation where you are passing data from a parent to a child component, then to a grandchild component, and so on, until it reaches a more distant component further down the component tree, where this data is required
- Prop drilling is a situation where you are passing data from a child, to a parent component, then to a grandparent component, and so on, until it reaches a more distant component further up the component tree, where this data is required.
36. The distinction between stateful and stateless components is that the latter doesn't have its own state.
- True
- False
37. Choose the correct statement.
- Remember that you should always change the values of props in children components; you should never work with them as they are. In other words, props are mutable.
- Remember that you should never change the values of props in children components; you should only work with them as they are. In other words, props are immutable.
38. Is this code valid?
function App() {
const handler = () => console.log('fourth example')
return (
< div>
< button onClick = {handler} >
Click Me!
< /button>
< /div>
)
}
export default App
- Yes
- No
39. Is this code valid?
< button onClick={ () => console.log('clicked') }>
Click me
< /button>
- Yes
- No
40. Select the correct statement: The useState hook...
- โฆ lets you hook into React state and lifecycle features from a component.
- โฆis not part of React; you must import it from a third-party package.
- โฆ has a convention that if the state variable is named, for example, counter, the function to update this counter variable should be named counterFunction.
- โฆ should never be called at the top level of a React component.
41. The Context API allows you to:
- Avoid having to pass state down through multiple levels of components.
- Avoid having to use the return statement in a child component.
- Avoid having to keep your components modular.