React Portals and Refs Quiz

Create an account and save your quiz results

Login and save your results

OR

Question 1/15

Which of the following statements about React refs is true?

Select your answer

Question 2/15

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

Select your answer

Question 3/15

Can React portals be used in server-side rendering applications?

Select your answer

Question 4/15

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

Select your answer

Question 5/15

What should you consider when passing refs to child components?

Select your answer

Question 6/15

Why might you use the useImperativeHandle hook with forwardRef?

Select your answer

Question 7/15

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

Select your answer

Question 8/15

Can a functional component use multiple refs for different purposes?

Select your answer

Question 9/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 10/15

Should refs be used to expose component functions as a public API?

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

Which DOM event does not automatically propagate through React portals?

Select your answer

Question 13/15

What is a common limitation of React portals?

Select your answer

Question 14/15

What is the main advantage of using React portals?

Select your answer

Question 15/15

Which method allows you to create refs in class components?

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
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 2/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 3/15
😊 Your answer was correct 🙁 Your answer was incorrect
Can React portals be used in server-side rendering applications?

Available answers

While react portals can be used with server-side rendering, they require special care for client-side hydration to ensure the portal content is correctly matched and rendered both server-side and client-side.
Question 4/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.
Question 5/15
😊 Your answer was correct 🙁 Your answer was incorrect
What should you consider when passing refs to child components?

Available answers

To pass a ref to a child component, you need to use
React.forwardRef
to ensure the child component can receive it properly and forward it to a DOM node or another child if necessary.
Question 6/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 7/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 8/15
😊 Your answer was correct 🙁 Your answer was incorrect
Can a functional component use multiple refs for different purposes?

Available answers

In functional components, you can create multiple refs using
React.useRef()
for different purposes, such as managing DOM access for different elements.
Question 9/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 10/15
😊 Your answer was correct 🙁 Your answer was incorrect
Should refs be used to expose component functions as a public API?

Available answers

Refs can be used to expose certain methods/variables via the
ref
API to parent components, enabling functionalities like focusing an input from the parent or playing/pausing a video.
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
Which DOM event does not automatically propagate through React portals?

Available answers

Certain events like
onWheel
may not propagate from elements rendered in a portal to their bounding DOM elements due to event capture behavior.
Question 13/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 14/15
😊 Your answer was correct 🙁 Your answer was incorrect
What is the main advantage of using React portals?

Available answers

React portals enable components to be rendered outside the parent DOM hierarchy, helping to avoid layout issues, especially with components like modals or tooltips.
Question 15/15
😊 Your answer was correct 🙁 Your answer was incorrect
Which method allows you to create refs in class components?

Available answers

In class components, the
React.createRef()
method is used to create refs that can be attached to React elements to gain direct access to DOM elements or React components.