-
import React.Component from 'react'
-
import [ Component ] from 'react'
-
import Component from 'react'
-
import { 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.
const person =(firstName, lastName) =>
{
first: firstName,
last: lastName
}
console.log(person("Jill", "Wilson"))
- Wrap the object in parentheses.
- Call the function from another file.
- Add a return statement before the first curly brace.
- Replace the object with an array.
Q4. If you see the following import in a file, what is being used for state management in the component?
import React, {useState} from 'react';
- 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);
-
{{name: "Rachel", age: 31}}
-
{name: "Rachel", age: 31}
-
{person: "Rachel", person: 31}}
-
{person: {name: "Rachel", age: 31}}
- Mocha
- Chai
- Sinon
- Jest
Q7. To get the first item from the array ("cooking") using array destructuring, how do you adjust this line?
const topics = ['cooking', 'art', 'history'];
-
const first = ["cooking", "art", "history"]
-
const [] = ["cooking", "art", "history"]
-
const [, first]["cooking", "art", "history"]
-
const [first] = ["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
const [, , animal] = ['Horse', 'Mouse', 'Cat'];
console.log(animal);
- Horse
- Cat
- Mouse
- undefined
- JSX Editor
- ReactDOM
- Browser Buddy
- Babel
- 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
<Message {...props} />
- any that have not changed
- all of them
- child props
- any that have changed
<Route path="/:id" />
- This is a route modal
- This is a route parameter
- This is a route splitter
- This is a route link
14. If you created a component called Dish and rendered it to the DOM, what type of element would be rendered?
function Dish() {
return <h1>Mac and Cheese</h1>;
}
ReactDOM.render(<Dish />, document.getElementById('root'));
-
div
- section
- component
-
h1
React.createElement('h1', null, "What's happening?");
-
<h1 props={null}>What's happening?</h1>
-
<h1>What's happening?</h1>
-
<h1 id="component">What's happening?</h1>
-
<h1 id="element">What's happening?</h1>
16. 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
const message = 'Hi there';
const element = <p>{message}</p>;
- a JS function
- a JS element
- a JS expression
- a JSX wrapper
-
React.memo
-
React.split
-
React.lazy
-
React.fallback
- 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
20. 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.
function Dish(props) {
return <h1>{props.name} {props.cookingTime}</h1>;
}
-
function Dish([name, cookingTime]) { return <h1>{name} {cookingTime}</h1>; }
-
function Dish({name, cookingTime}) { return <h1>{name} {cookingTime}</h1>; }
-
function Dish(props) { return <h1>{name} {cookingTime}</h1>; }
-
function Dish(...props) { return <h1>{name} {cookingTime}</h1>; }
- when you do not want your component to have props
- when you have sibling components that need to be compared
- when you want a default implementation of
shouldComponentUpdate()
- when you do not want your component to have state
23. Why is it important to avoid copying the values of props into a component's state where possible?
- because you should never mutate state
- because
getDerivedStateFromProps()
is an unsafe method to use - because you want to allow a component to update in response to changes in the props
- because you want to allow data to flow back up to the parent
- a property that adds child components to state
- a property that lets you pass components as data to other components
- a property that lets you set an array as a property
- a property that lets you pass data to child elements
- injectHTML
- dangerouslySetInnerHTML
- weirdSetInnerHTML
- strangeHTML
- declarative
- integrated
- closed
- imperative
- to put together physical file folders
- to preprocess files
- to load external data
- to load the website into everyone's phone
28. A representation of a user interface that is kept in memory and is synced with the "real" DOM is called what?
- virtual DOM
- DOM
- virtual elements
- shadow DOM
const Heading = () => {
<h1>Hello!</h1>;
};
- Add a render function.
- Change the curly braces to parentheses or add a return statement before the
h1
tag. - Move the
h1
to another component. - Surround the
h1
in adiv
.
- const
- let
- constant
- var
Q31. What do you call a React component that catches JavaScript errors anywhere in the child component tree?
- error bosses
- error catchers
- error helpers
- error boundaries
- constructor
- componentDidMount
- componentWillReceiveProps
- componentWillMount
- by putting them in the same file
- by nesting components
- with webpack
- with code splitting
- monads
- pure functions
- recursive functions
- higher-order functions
- to directly access the DOM node
- to refer to another JS file
- to call a function
- to bind the function
handleChange(e) {
this.setState({ [e.target.id]: e.target.value })
}
- a computed property name
- a set value
- a dynamic key
- a JSX code string
class Clock extends React.Component {
render() {
return <h1>Look at the time: {time}</h1>;
}
}
- Clock
- It does not have a name prop.
- React.Component
- Component
- 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
- 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.
-
React
-
ReactDOM
-
Render
-
DOM
- Use the
value
property. - Use the
defaultValue
property. - Use the
default
property. - It assigns one automatically.
class clock extends React.Component {
render() {
return <h1>Look at the time: {this.props.time}</h1>;
}
}
- Add quotes around the return value
- Remove
this
- Remove the render method
- Capitalize
clock
Explanation
In JSX, lower-case tag names are considered to be HTML tags. Read [this article](https://reactjs.org/docs/jsx-in-depth.html#html-tags-vs.-react-components)
-
useEffect(function updateTitle() { document.title = name + ' ' + lastname; });
-
useEffect(() => { title = name + ' ' + lastname; });
-
useEffect(function updateTitle() { name + ' ' + lastname; });
-
useEffect(function updateTitle() { title = name + ' ' + lastname; });
-
React.fallback
-
React.split
-
React.lazy
-
React.memo
function MyComponent(props) {
const [done, setDone] = useState(false);
return <h1>Done: {done}</h1>;
}
-
useEffect(() => { setDone(true); });
-
useEffect(() => { setDone(true); }, []);
-
useEffect(() => { setDone(true); }, [setDone]);
-
useEffect(() => { setDone(true); }, [done, setDone]);
class Huggable extends React.Component {
hug(id) {
console.log("hugging " + id);
}
render() {
let name = "kitteh";
let button = // Missing Code
return button;
}
}
-
<button onClick={(name) => this.hug(name)>Hug Button</button>
-
<button onClick={this.hug(e, name)}>Hug Button</button>
-
<button onClick={(e) => hug(e, name)}>Hug Button</button>
-
<button onClick={(e) => this.hug(name, e)}>Hug Button</button>
<button onClick={this.handleClick()}>Click this</button>
-
<button onClick={this.handleClick.bind(handleClick)}>Click this</button>
-
<button onClick={handleClick()}>Click this</button>
-
<button onClick={this.handleClick}>Click this</button>
-
<button onclick={this.handleClick}>Click this</button>
- A function component is the same as a class component.
- A function component accepts a single props object and returns a React element.
- A function component is the only way to create a component.
- A function component is required to create a React component.
- FetchJS
- ReactDOM
- No library.
fetch()
is supported by most browsers. - React
Q50. What will happen when this useEffect Hook is executed, assuming name is not already equal to John?
useEffect(() => {
setName("John");
}, [name]);
- It will cause an error immediately.
- It will execute the code inside the function, but only after waiting to ensure that no other component is accessing the name variable.
- It will update the value of name once and not run again until name is changed from the outside.
- It will cause an infinite loop.
- if the component calls
this.setState(...)
- the value of one of the component's props changes
- if the component calls
this.forceUpdate()
- one of the component's siblings rerenders
Q52. You have created a new method in a class component called handleClick, but it is not working. Which code is missing?
class Button extends React.Component{
constructor(props) {
super(props);
// Missing line
}
handleClick() {...}
}
-
this.handleClick.bind(this);
-
props.bind(handleClick);
-
this.handleClick.bind();
-
this.handleClick = this.handleClick.bind(this);
Q53. 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>
Q54. 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
const greeting = isLoggedIn ? <Hello /> : null;
- never
- when
isLoggedIn
is true - when a user logs in
- when the Hello function is called
ReactDOM.render(
<Message orderNumber="16"/>,
document.getElementById("root")
);
- string
- boolean
- object
- number
Q57. 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>;
-
const element = <h1 style="backgroundColor: "blue""}>Hi</h1>;
-
const element = <h1 style={{backgroundColor: "blue"}}>Hi</h1>;
-
const element = <h1 style={blue}>Hi</h1>;
-
const element = <h1 style="blue">Hi</h1>;
-
replaceState
-
refreshState
-
updateState
-
setState
const Star = ({ selected = false }) => (
<Icon color = {selected ? "red" : "grey"} />
);
- black
- red
- grey
- white