Skip to the content.

React Multiple Choice Questions


Q. If you want to import just the Component from the React library, what syntax do you use?

↥ back to top

Q. If a function component should always render the same way given the same props, what is a simple performance optimization available for it?

↥ back to top

Q. 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"))
↥ back to top

Q. If you see the following import in a file, what is being used for state management in the component?

import React, {useState} from 'react';

↥ back to top

Q. 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);
↥ back to top

Q. What is the testing library most often associated with React?

↥ back to top

Q. To get the first item from the array (“cooking”) using array destructuring, how do you adjust this line?

const topics = ['cooking', 'art', 'history'];
↥ back to top

Q. How do you handle passing through the component tree without having to pass props down manually at every level?

↥ back to top

Q. What should the console read when the following code is run?

const [, , animal] = ['Horse', 'Mouse', 'Cat'];
console.log(animal);
↥ back to top

Q. What is the name of the tool used to take JSX and turn it into createElement calls?

↥ back to top

Q. Why might you use useReducer over useState in a React component?

↥ back to top

Q. Which props from the props object is available to the component with the following syntax?

<Message {...props} />
↥ back to top

Q. Consider the following code from React Router. What do you call :id in the path prop?

<Route path="/:id" />
↥ back to top

Q. 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'));
↥ back to top

Q. 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?");
↥ back to top

Q. 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>
  );
}
↥ back to top

Q. What do you call the message wrapped in curly braces below?

const message = 'Hi there';
const element = <p>{message}</p>;
↥ back to top

Q. What can you use to handle code splitting?

↥ back to top

Q. When do you use useLayoutEffect?

↥ back to top

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!

Q. 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>
↥ back to top

Q. How do you destructure the properties that are sent to the Dish component?

function Dish(props) {
  return (
    <h1>
      {props.name} {props.cookingTime}
    </h1>
  );
}
↥ back to top

Q. When might you use React.PureComponent?

↥ back to top

Q. Why is it important to avoid copying the values of props into a component’s state where possible?

↥ back to top

Q. What is the children prop?

↥ back to top

Q. Which attribute is React’s replacement for using innerHTML in the browser DOM?

↥ back to top

Q. Which of these terms commonly describe React applications?

↥ back to top

Q. When using webpack, why would you need to use a loader?

↥ back to top

Q. A representation of a user interface that is kept in memory and is synced with the “real” DOM is called what?

↥ back to top

Q. You have written the following code but nothing is rendering. How do you fix this problem?

const Heading = () => {
  <h1>Hello!</h1>;
};
↥ back to top

Q. To create a constant in JavaScript, which keyword do you use?

↥ back to top

Q. What do you call a React component that catches JavaScript errors anywhere in the child component tree?

↥ back to top

Q. In which lifecycle method do you make requests for data in a class component?

↥ back to top

Q. React components are composed to create a user interface. How are components composed?

↥ back to top

Q. All React components must act like _ with respect to their props.

↥ back to top

Q. What is [e.target.id] called in this code snippet?

const handleChange = (e) => {
  setState((prevState) => ({ ...prevState, [e.target.id]: e.target.value }));
};
↥ back to top

Q. What is the name of this component?

class Clock extends React.Component {
  render() {
    return <h1>Look at the time: {time}</h1>;
  }
}
↥ back to top

Q. What is sent to an Array.map() function?

↥ back to top

Q. Why is it a good idea to pass a function to setState instead of an object?

↥ back to top

Explanation: Because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state.

Q. What package contains the render() function that renders a React element tree to the DOM?

↥ back to top

Q. How do you set a default value for an uncontrolled form field?

↥ back to top

Q. 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>;
};
↥ back to top

Explanation: In JSX, lower-case tag names are considered to be HTML tags.

Q. Which Hook could be used to update the document’s title?

↥ back to top

Q. Which function from React can you use to wrap Component imports to load them lazily?

↥ back to top

Q. How do you invoke setDone only when component mounts, using hooks?

function MyComponent(props) {
  const [done, setDone] = useState(false);

  return <h1>Done: {done}</h1>;
}
↥ back to top

Q. Currently, handleClick is being called instead of passed as a reference. How do you fix this?

<button onClick={this.handleClick()}>Click this</button>
↥ back to top

Q. Which answer best describes a function component?

↥ back to top

Q. Which library does the fetch() function come from?

↥ back to top

Q. What will happen when this useEffect Hook is executed, assuming name is not already equal to John?

useEffect(() => {
  setName('John');
}, [name]);
↥ back to top

Q. Which choice will not cause a React component to rerender?

↥ back to top

Q. 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() {...}
}
↥ back to top

Q. 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>
<...>
  <h1>Our Staff</h1>
  <p>Our staff is available 9-5 to answer your questions</p>
</...>
<//>
  <h1>Our Staff</h1>
  <p>Our staff is available 9-5 to answer your questions</p>
<///>
<>
  <h1>Our Staff</h1>
  <p>Our staff is available 9-5 to answer your questions</p>
</>
<Frag>
  <h1>Our Staff</h1>
  <p>Our staff is available 9-5 to answer your questions</p>
</Frag>
↥ back to top

Q. 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>;
  }
}
↥ back to top

Q. Per the following code, when is the Hello component assigned to greeting?

const greeting = isLoggedIn ? <Hello /> : null;
↥ back to top

Q. In the following code block, what type is orderNumber?

ReactDOM.render(<Message orderNumber="16" />, document.getElementById('root'));
↥ back to top

Q. 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>;
↥ back to top

Q. Which function is used to update state variables in a React class component?

↥ back to top

Q. Consider the following component. What is the default color for the star?

const Star = ({ selected = false }) => <Icon color={selected ? 'red' : 'grey'} />;
↥ back to top

Q. 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>
↥ back to top

Q. How would you add to this code, from React Router, to display a component called About?

<Route path="/:id" />
<Route path="/:id">
  {' '}
  <About />
</Route>
<Route path="/tid" about={Component} />
<Route path="/:id" route={About} />
<Route>
  <About path="/:id" />
</Route>
↥ back to top

Q. Which class-based component is equivalent to this function component?

const Greeting = ({ name }) => <h1>Hello {name}!</h1>;
class Greeting extends React.Component {
  constructor() {
    return <h1>Hello {this.props.name}!</h1>;
  }
}
class Greeting extends React.Component {
  <h1>Hello {this.props.name}!</h1>;
}
class Greeting extends React.Component {
  render() {
    return <h1>Hello {this.props.name}!</h1>;
  }
}
class Greeting extends React.Component {
  render({ name }) {
    return <h1>Hello {name}!</h1>;
  }
}
↥ back to top

Q. 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')
)
↥ back to top
↥ back to top

Q. What is the first argument, x, that is sent to the createElement function?

React.createElement(x, y, z);
↥ back to top

Q. Which class-based lifecycle method would be called at the same time as this effect Hook?

useEffect(() => {
  // do things
}, []);
↥ back to top

Q. 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>;
  }
}
↥ back to top

Q. When using a portal, what is the first argument?

ReactDOM.createPortal(x, y);
↥ back to top

Q. What is setCount?

const [count, setCount] = useState(0);
↥ back to top

Q. What is the use of map function below?

const database = [{ data: 1 }, { data: 2 }, { data: 3 }];
database.map((user) => <h1>{user.data}</h1>);
↥ back to top

Q. Describe what is happening in this code?

const { name: firstName } = props;
↥ back to top

Q. What is wrong with this code?

const MyComponent = ({ names }) => (
  <h1>Hello</h1>
  <p>Hello again</p>
);
↥ back to top

Q. When using a portal, what is the second argument?

ReactDOM.createPortal(x, y);
↥ back to top

Q. Given this code, what will be printed in the <div> tag?

const MyComponent = ({ children }) => (
  <div>{children.length}</div>
);
...
<MyComponent>
<p>
  Hello <span>World!</span>
</p>
<p>Goodbye</p>
</MyComponent>
↥ back to top

Q. What is this pattern called?

const [count, setCount] = useState(0);
↥ back to top

Q. What is the first file loaded by the browser in a basic React project?

↥ back to top

Q. The code below is rendering nothing and generate this error: “ReactDOM is not defined.” How do you fix this issue?

import React from 'react';
import { createRoot } from 'reactjs-dom';

const element = <h1>Hi</h1>;
// Note: error on the line below
const root = ReactDOM.createRoot(document.getElementById('root'));

root.render(element);
↥ back to top

Q. 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>
  );
}
↥ back to top

Q. You are rendering a list with React when this warning appears in the console: “Warning: Each child in a list should have a unique ‘key’ prop.” How do you fix this issue?

↥ back to top

Q. How would you generate the boilerplate code for a new app that you are building to collect underpants?

↥ back to top

Q. Add the code that will fire the photon torpedoes when the button is clicked.

class StarTrekkin extends React.Component {
  firePhotonTorpedoes(e) {
    console.log('pew pew');
  }
  render() {
    return; // Missing code
  }
}
↥ back to top

Q. What is the process of deciding whether an update is necessary?

↥ back to top

Q. React is an open-source project but is maintained by which company?

↥ back to top

Q. What command can you use to generate a React project?

↥ back to top

Q. What is the browser extension called that React developers use to debug applications?

↥ back to top

Q. Which tool is not part of Create React App?

↥ back to top

Q. What is the JavaScript syntax extension that is commonly used to create React elements?

↥ back to top

Q. How might you check property types without using Flow or TypeScript?

↥ back to top

Q. How do you add an id of heading to the following h1 element?

let dish = <h1>Mac and Cheese</h1>;
↥ back to top

Q. 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.

↥ back to top

Q. What syntax do you use to create a component in React?

↥ back to top

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.

Q. You want to disable a button so that it does not emit any events onClick. Which prop do you use to acomplish this?

↥ back to top

Q. In this function, which is the best way to describe the Dish component?

function Dish() {
  return (
    <>
      <Ingredient />
      <Ingredient />
    </>
  );
}
↥ back to top

Q. When does the componentDidMount function fire?

↥ back to top

Q. What might you use webpack for in React development?

↥ back to top

Q. When using the React Developer Tools Chrome extension, what does it mean if the React icon is red?

↥ back to top

Q. 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) ;
  }
  ...
}
↥ back to top

Q. Which language can you not use with React?

↥ back to top

Q. 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: []
    };
}
↥ back to top

Q. What would be the result of running this code?

function add(x = 1, y = 2) {
  return x + y;
}

add();

image

↥ back to top

Q. Why might you use a React.ref?

↥ back to top

Q. What pattern is being used in this code block?

const { tree, lake } = nature;
↥ back to top

Q. 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")
);
<Message sent={false} />,
ReactDom.render(<Message sent="false" />, document.getElementById('root'));
<Message sent="false" />,
ReactDom.render(<Message sent="false" />, document.getElementById('root'));
↥ back to top

Q. 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([]);
  /// ...
};
↥ back to top

Q. When using a portal, what is the second argument?

ReactDOM.createPortal(x, y);
↥ back to top

Q. What would you pass to the onClick prop that wil allow you to pass the initName prop into the greeet handler?

const Greeting = ({ initName }) => {
  const greet = (name) => console.log("Hello, " + name + "!");
  return <button onClick={ ... }>Greeting Button </button>
}

Explanation: Apparently the question misstyped greet as hug. Putting this aside, we can still learn from this question.

↥ back to top

Q. What is the name of the compiler used to transform JSX into JavaScript?

↥ back to top

Q. Which hook is used to prevent a function from being recreated on every component render?

↥ back to top

Q. Why might you use the useRef hook?

↥ back to top

Q. Which of the following is required to use React?

↥ back to top

Q. What is the correct way to get a value from context?

↥ back to top