React Portals and Refs Quiz

Create an account and save your quiz results

Login and save your results

OR

Question 1/15

When using useImperativeHandle, what is passed to the hook to expose methods to the parent component?

Select your answer

Question 2/15

Which hook is commonly used with React refs to perform an action after the component has been updated?

Select your answer

Question 3/15

What is the main difference between React.createRef and React.useRef?

Select your answer

Question 4/15

How do portals handle propagation of parent CSS styles?

Select your answer

Question 5/15

If a portal is rendered inside a component, will the state updates of that component affect the portal?

Select your answer

Question 6/15

What is a common limitation of React portals?

Select your answer

Question 7/15

Which of the following statements about React refs is true?

Select your answer

Question 8/15

How does state update affect a component with refs?

Select your answer

Question 9/15

Why might you use the useImperativeHandle hook with forwardRef?

Select your answer

Question 10/15

Is it possible to use refs for callback purposes?

Select your answer

Question 11/15

How can a functional component update its ref in response to a state change?

Select your answer

Question 12/15

What is a scenario where using refs is preferred over component state?

Select your answer

Question 13/15

Can a React component be mounted into multiple portals simultaneously?

Select your answer

Question 14/15

What value do you get when you access a ref using ref.current immediately after creating it?
const myRef = React.useRef();
console.log(myRef.current);

Select your answer

Question 15/15

How does React handle cleanup for refs during unmounting of the component?

Select your answer

Your Results

You did not answer any questions correctly.

Your Answers

Question 1/15
😊 Your answer was correct 🙁 Your answer was incorrect
When using <code>useImperativeHandle</code>, what is passed to the hook to expose methods to the parent component?

Available answers

useImperativeHandle takes a ref and an imperative handle function that returns an object containing the properties and methods to expose to the parent.
Question 2/15
😊 Your answer was correct 🙁 Your answer was incorrect
Which hook is commonly used with React refs to perform an action after the component has been updated?

Available answers

The
useEffect
hook is commonly used with refs when you want to perform an action after the component has been updated, such as interacting with an element rendered through a ref.
Question 3/15
😊 Your answer was correct 🙁 Your answer was incorrect
What is the main difference between <code>React.createRef</code> and <code>React.useRef</code>?

Available answers

React.createRef
is usually used in class components and returns a new ref object on every render, while
React.useRef
is used in functional components and retains the same ref object across renders.
Question 4/15
😊 Your answer was correct 🙁 Your answer was incorrect
How do portals handle propagation of parent CSS styles?

Available answers

React portals don't automatically inherit CSS from their parents, as they're set in a separate DOM node. Manual styling or styled-components/global CSS should be considered.
Question 5/15
😊 Your answer was correct 🙁 Your answer was incorrect
If a portal is rendered inside a component, will the state updates of that component affect the portal?

Available answers

React portals, although rendered outside the DOM hierarchy, are still a part of the parent component's React tree, meaning they will update whenever the parent component updates.
Question 6/15
😊 Your answer was correct 🙁 Your answer was incorrect
What is a common limitation of React portals?

Available answers

Event bubbling through portals occurs as if the portal elements were stuck to the parent component that created them, not the DOM hierarchy where they are placed, which can confuse handling events.
Question 7/15
😊 Your answer was correct 🙁 Your answer was incorrect
Which of the following statements about React refs is true?

Available answers

Refs are particularly useful for storing mutable data that does not require re-rendering of the component when changed, like DOM manipulation.
Question 8/15
😊 Your answer was correct 🙁 Your answer was incorrect
How does state update affect a component with refs?

Available answers

Refs are distinct from the state and are not affected by state updates. The values within a ref hold constant through component re-renders unless explicitly changed.
Question 9/15
😊 Your answer was correct 🙁 Your answer was incorrect
Why might you use the <code>useImperativeHandle</code> hook with <code>forwardRef</code>?

Available answers

The
useImperativeHandle
hook customizes the instance value exposed to parent components when a ref is used, enabling optimizations and additional logic.
Question 10/15
😊 Your answer was correct 🙁 Your answer was incorrect
Is it possible to use refs for callback purposes?

Available answers

Callback refs can be used in React to dynamically determine how a ref property is assigned, allowing more flexibility in setting refs based on certain conditions or side-effects.
Question 11/15
😊 Your answer was correct 🙁 Your answer was incorrect
How can a functional component update its ref in response to a state change?

Available answers

To respond to state changes with a ref, you set up logic in an effect hook that can modify the ref variable upon changes in the component's state.
Question 12/15
😊 Your answer was correct 🙁 Your answer was incorrect
What is a scenario where using refs is preferred over component state?

Available answers

For tasks like accessing or modifying DOM elements directly such as animations, using refs is preferred because these updates do not require triggering re-renders.
Question 13/15
😊 Your answer was correct 🙁 Your answer was incorrect
Can a React component be mounted into multiple portals simultaneously?

Available answers

A React component can only be instantiated once in the application tree, so it can't be rendered into multiple portals simultaneously.
Question 14/15
😊 Your answer was correct 🙁 Your answer was incorrect
What value do you get when you access a ref using <code>ref.current</code> immediately after creating it?
const myRef = React.useRef();
console.log(myRef.current);

Available answers

Initially, the
current
property of a ref object is set to
null
. It will be updated to point to the DOM element after the first render.
Question 15/15
😊 Your answer was correct 🙁 Your answer was incorrect
How does React handle cleanup for refs during unmounting of the component?

Available answers

Refs are automatically garbage collected when a component unmounts, as they are simply pointers to DOM elements or storage, so no explicit cleanup is needed.