Reactjs QB
Reactjs QB
Reactjs QB
js
Q1. If you want to import just the Component from the React library, what syntax
do you use?
import React.Component from 'react'
Q2. If a function component should always render the same way given the same
props, what is a simple performance optimization available for it?
Wrap it in the React.memo higher-order component.
Implement the useReducer Hook.
Implement the useMemo Hook.
Implement the shouldComponentUpdate lifecycle method.
Q3. How do you fix the syntax error that results from running this code?
const person =(firstName, lastName) =>
{
first: firstName,
last: lastName
}
console.log(person("Jill", "Wilson"))
React Hooks
stateful components
math
class components
Q5. Using object literal enhancement, you can put values back into an object.
When you log person to the console, what is the output?
const name = 'Rachel';
const age = 31;
const person = { name, age };
console.log(person);
Q6. What is the testing library most often associated with React?
Mocha
Chai
Sinon
Jest
Reference
Q7. To get the first item from the array ("cooking") using array destructuring, how
do you adjust this line?
const topics = ['cooking', 'art', 'history'];
Q8. How do you handle passing through the component tree without having to
pass props down manually at every level?
React Send
React Pinpoint
React Router
React Context
Reference
Q9. What should the console read when the following code is run?
const [, , animal] = ['Horse', 'Mouse', 'Cat'];
console.log(animal);
Horse
Cat
Mouse
undefined
Q10. What is the name of the tool used to take JSX and turn it into createElement
calls?
JSX Editor
ReactDOM
Browser Buddy
Babel
Q11. Why might you use useReducer over useState in a React component?
when you want to replace Redux
when you need to manage more complex state in an app
when you want to improve performance
when you want to break your production app
Q12. Which props from the props object is available to the component with the
following syntax?
<Message {...props} />
div
section
component
h1
Q15. What does this React element look like given the following function?
(Alternative: Given the following code, what does this React element look like?)
React.createElement('h1', null, "What's happening?");
<h1>What's happening?</h1>
Reference
Q16. What property do you need to add to the Suspense component in order to
display a spinner or loading state?
function MyComponent() {
return (
<Suspense>
<div>
<Message />
</div>
</Suspense>
);
}
lazy
loading
fallback
spinner
Reference
Q17. What do you call the message wrapped in curly braces below?
const message = 'Hi there';
const element = <p>{message}</p>;
a JS function
a JS element
a JS expression
a JSX wrapper
Q18. What can you use to handle code splitting?
React.memo
React.split
React.lazy
React.fallback
Reference
Q19. When do you use useLayoutEffect ?
to optimize for all devices
to complete the update
to change the layout of the screen
when you need the browser to paint before the effect runs
Reference
Answer confirmed by multiple members of the community in this internal discussion
Explanation: useLayoutEffect gets executed before the useEffect hook without
much concern for DOM mutation. Even though the React hook useLayoutEffect is
set after the useEffect Hook, it gets triggered first!
Q20. What is the difference between the click behaviors of these two buttons
(assuming that this.handleClick is bound correctly)?
A. <button onClick={this.handleClick}>Click Me</button>
B. <button onClick={event => this.handleClick(event)}>Click Me</button>
Button A will not have access to the event object on click of the button.
Button B will not fire the handler this.handleClick successfully.
Button A will not fire the handler this.handleClick successfully.
There is no difference.
Q21. How do you destructure the properties that are sent to the Dish component?
function Dish(props) {
return (
<h1>
{props.name} {props.cookingTime}
</h1>
);
}
Clock
It does not have a name prop.
React.Component
Component
Q37. What is sent to an Array.map() function?
a callback function that is called once for each element in the array
the name of another array to iterate over
the number of times you want to call the function
a string describing what the function should do
Q38. Why is it a good idea to pass a function to setState instead of an object?
It provides better encapsulation.
It makes sure that the object is not mutated.
It automatically updates a component.
setState is asynchronous and might result in out of sync values.
Reference
Explanation: Because this.props and this.state may be updated
asynchronously, you should not rely on their values for calculating the next state.
Q39. What package contains the render() function that renders a React element
tree to the DOM?
React
ReactDOM
Render
DOM
Q40. How do you set a default value for an uncontrolled form field?
Use the value property.
Use the defaultValue property.
Use the default property.
It assigns one automatically.
Q41. What do you need to change about this code to get it to run?
const clock = (props) => {
return <h1>Look at the time: {props.time}</h1>;
};
Q43. Which function from React can you use to wrap Component imports to load
them lazily?
fallback
split
lazy
memo
Reference
Q44. How do you invoke setDone only when component mounts, using hooks?
function MyComponent(props) {
const [done, setDone] = useState(false);
<button onClick={this.handleClick.bind(handleClick)}>Click
this</button>
constructor(props) {
super(props);
// Missing line
}
handleClick() {...}
}
this.handleClick.bind(this);
props.bind(handleClick);
this.handleClick.bind();
this.handleClick = this.handleClick.bind(this);
Q51. React does not render two sibling elements unless they are wrapped in a
fragment. Below is one way to render a fragment. What is the shorthand for this?
<React.Fragment>
<h1>Our Staff</h1>
<p>Our staff is available 9-5 to answer your questions</p>
</React.Fragment>
A
<...>
<h1>Our Staff</h1>
<p>Our staff is available 9-5 to answer your questions</p>
</...>
B
<//>
<h1>Our Staff</h1>
<p>Our staff is available 9-5 to answer your questions</p>
<///>
C
<>
<h1>Our Staff</h1>
<p>Our staff is available 9-5 to answer your questions</p>
</>
D
<Frag>
<h1>Our Staff</h1>
<p>Our staff is available 9-5 to answer your questions</p>
</Frag>
Q52. If you wanted to display the count state value in the component, what do you
need to add to the curly braces in the h1 ?
class Ticker extends React.component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
render() {
return <h1>{}</h1>;
}
}
this.state.count
count
state
state.count
Q53. Per the following code, when is the Hello component assigned to greeting?
const greeting = isLoggedIn ? <Hello /> : null;
never
when isLoggedIn is true
when a user logs in
when the Hello function is called
Q54. In the following code block, what type is orderNumber?
ReactDOM.render(<Message orderNumber="16" />, document.getElementById('roo
string
boolean
object
number
Q55. You have added a style property to the h1 but there is an unexpected token
error when it runs. How do you fix this?
const element = <h1 style={ backgroundColor: "blue" }>Hi</h1>;
refreshState
updateState
setState
Q57. Consider the following component. What is the default color for the star?
const Star = ({ selected = false }) => <Icon color={selected ? 'red' : 'gr
black
red
grey
white
Q58. What is the difference between the click behaviors of these two
buttons(assuming that this.handleClick is not bound correctly)
A. <button onClick=this.handleClick>Click Me</button>
B. <button onClick={event => this.handleClick(event)}>Click Me</button>
Button A will not have access to the event object on click of the
button
There is no difference
B
<Route path="/tid" about={Component} />
C
<Route path="/:id" route={About} />
D
<Route>
<About path="/:id" />
</Route>
Reference
Q60. Which class-based component is equivalent to this function component?
const Greeting = ({ name }) => <h1>Hello {name}!</h1>;
A
class Greeting extends React.Component {
constructor() {
return <h1>Hello {this.props.name}!</h1>;
}
}
B
class Greeting extends React.Component {
<h1>Hello {this.props.name}!</h1>;
}
C
class Greeting extends React.Component {
render() {
return <h1>Hello {this.props.name}!</h1>;
}
}
D
class Greeting extends React.Component {
render({ name }) {
return <h1>Hello {name}!</h1>;
}
}
Q61. Give the code below, what does the second argument that is sent to the
render function describe?
ReactDOM.render(
<h1>Hi<h1>,
document.getElementById('root')
)
componentWillUnmount
componentDidMount
render
componentDidUpdate
Reference
Q65. What is the name of the base component of this component?
class Comp extends React.Component {
render() {
return <h1>Look at the time: {time}</h1>;
}
}
Comp
h1
React.Component
Component
Q66. When using a portal, what is the first argument?
ReactDOM.createPortal(x, y);
It is creating a new object that contains the same name property as the props
object.
It is assigning the value of the props object's firstName property to a constant
called name.
It is retrieving the value of props.name.firstName.
It is assigning the value of the props object's name property to a constant called
firstName.
Q70. What is wrong with this code?
const MyComponent = ({ names }) => (
<h1>Hello</h1>
<p>Hello again</p>
);
object destructuring
array destructuring
spread operating
code pushing
Q74. What is the first file loaded by the browser in a basic React project?
src/App.js
src/index.js
public/manifest.json
public/index.html
Q75. The code below is rendering nothing and generates this error: "ReactDOM is
not defined." How do you fix this issue?
import React from 'react';
import { createRoot } from 'reactjs-dom';
root.render(element);
createRoot(document.getElementById("root"));
ReactDOM(element, document.getElementById("root"));
renderDOM(element, document.getElementById("root"));
DOM(element, document.getElementById("root"));
Q76. In this component, how do you display whether the user was logged in or
not?
render() {
const isLoggedIn = this.state.isLoggedIn;
return (
<div>
The user is:
</div>
);
}
Q88. What value of button will allow you to pass the name of the person to be
hugged?
class Huggable extends React.Component {
hug(id) {
console.log("hugging " + id);
}
render() {
let name = "kitten";
let button = // Missing code
return button;
}
}
Explanation: This question test knowledge of react class components. You need to
use this in order to call methods declared inside class components.
Q89. What syntax do you use to create a component in React?
a generator
a function or a class
a service worker
a tag
Reference
Explanation: React Components are like functions that return HTML elements.
Components are independent and reusable bits of code. They serve the same
purpose as JavaScript functions, but work in isolation and return HTML. Components
come in two types, Class components and Function components.
Q90. You want to disable a button so that it does not emit any events onClick.
Which prop do you use to acomplish this?
onBlur
onPress
defaultValue
disabled
Q91. In this function, which is the best way to describe the Dish component?
function Dish() {
return (
<>
<Ingredient />
<Ingredient />
</>
);
}
child component
parent component
nested component
sibling component
Q92. When does the componentDidMount function fire?
right after the component is added to the DOM
before the component is added to the DOM
right after the component is updated
right after an API call
Reference
Q93. What might you use webpack for in React development?
to fetch remote dependencies used by your app
to split your app into smaller chunks that can be more easily loaded by the
browser
to format your code so that it is more readable
to ensure your app is not vulnerable to code injection
Q94. When using the React Developer Tools Chrome extension, what does it mean
if the React icon is red?
You are using the development build of React.
You are using the production build of React.
You are using webpack.
You are using Create React App.
Reference
Q95. How would you modify the constructor to fix this error: "ReferenceError:
Must call super constructor in derived class before accessing 'this'..."?
class TransIsBeautiful extends React.Component {
constructor(props){
// Missing line
console.log(this) ;
}
...
}
render(props);
super(props);
super(this);
this.super();
Q96. Which language can you not use with React?
Swift.
JSX.
Javascipt.
TypeScript.
Q97. This code is part of an app that collects Pokemon. How would you print the
list of the ones collected so far?
constructor(props) {
super(props);
this.state = {
pokeDex: []
};
}
console.log(props.pokeDex);
console.log(this.props.pokeDex);
console.log(pokeDex);
console.log(this.state.pokeDex);
Reference
Q98. What would be the result of running this code?
function add(x = 1, y = 2) {
return x + y;
}
add();
null
3
0
undefined
function defaults
array destructuring
PRPL pattern
destructuring assignment
Reference
Q101. How would you correct this code block to make sure that the sent property
is set to the Boolean value false?
ReactDom.render(
<Message sent=false />,
document.getElementById("root")
);
A
<Message sent={false} />,
B
ReactDom.render(<Message sent="false" />, document.getElementById('root'))
C
<Message sent="false" />,
D
ReactDom.render(<Message sent="false" />, document.getElementById('root'))
Q102. This code is part of an app that collects Pokemon. The useState hook below
is a piece of state holding onto the names of the Pokemon collected so far. How
would you access the collected Pokemon in state?
const PokeDex = (props) => {
const [pokeDex, setPokeDex] = useState([]);
/// ...
};
props.pokeDex
this.props.pokeDex
setPokeDex()
pokeDex
Q103. What would you pass to the onClick prop that wil allow you to pass the
initName prop into the greet handler?
const Greeting = ({ initName }) => {
const greet = (name) => console.log("Hello, " + name + "!");
return <button onClick={ ... }>Greeting Button </button>
}
hug
this.hug(initName)
(name) => this.hug(name)
() => hug(initName)
Explanation: Apparently the question misstyped greet as hug . Putting this aside,
we can still learn from this question.
In a function, the global object is the default binding for this . In a browser
window the global object is [object Window]. This is a functional Component, so
this from this.hug actually refers to browser window. Since it is a functional
component, we can directly refer to hug without using this .
To pass a handler to onClick, we should always pass a function rather than
execute a function. So we need to use callback here. initName is available in
Greeting's function scope, so we can directly supply as an argument to hug().
Q104. What is the name of the compiler used to transform JSX into JavaScript?
Babel
JSX Editor
Browser Buddy
ReactDOM
Q105. Which hook is used to prevent a function from being recreated on every
component render?
useCallback
useMemo
useRef
useTransition
Q106. Why might you use the useRef hook?
To bind the function
To call a function
To directly access a DOM
To refer to another JS file
Reference
Q107. Which of the following is required to use React?
JavaScript
React Router
Redux
Prop-Types
Reference
Q108. What is the correct way to get a value from context?
const value = useContext(MyContext.Consumer)
const value = useContext(MyContext.Provider)
const value = useContext(MyContext)
const value = useContext({value: "intiial value"})
Reference
Q109. Why is ref used?
to bind function
to call function
to directly access DOM node
to refer to another JS file
Reference
Q110. Choose the method which should be overridden to stop the component from
updating?
componentDidMount
componentDidUpdate
willComponentUpdate
shouldComponentUpdate
Reference
Q111. What is the functionality of a “webpack” command?
Runs react local development server
Transfers all JS files to down into one file
A module builder
All of the above
Q112. Choose the method which is not a part of ReactDOM?
ReactDOM.createPortal()
ReactDOM.hydrate()
ReactDOM.destroy()
ReactDOM.findDOMnode()
Q113. In react, the key should be?
Unique among his siblings
Unique in DOM
Does not requires to be unique
all of the above
Reference
Q114. Which company developed ReactJS?
Google
Meta (ex Facebook)
Apple
Twitter
Reference
Q115. Choose the library which is most often associated with react?
Chai
Sinon
Jest
Mocha
Reference
Q116. What of the following is used in React.js to increase performance?
Original DOM
Virtual DOM
Both of the above
None of the above
Reference
Q117. Among The following options, choose the one which helps react for keeping
their data uni-directional?
DOM
flux
JSX
Props
Reference
Q118. Which choice is a correct refactor of the Greeting class component into a
function component?
class Greeting extends React.Component {
render() {
return <h1>Hello {this.props.name}!<h1>;
}
}
<ol>
{waitlist.map((name) => (
<li key={name}>{name}</li>
))}
</ol>
</div>
);
};