const modalRoot = document.getElementById('modal-root');
function Modal({ children }) {
return ReactDOM.createPortal(
children,
modalRoot
);
}
React Portals and Refs Quiz
Want to learn more than this quiz offers you? Have a look at my Frontend web
development courses.
Create an account and save your quiz results
Login and save your results
OR
Question 1/15
How do you create a portal in a React application?
const modalRoot = document.getElementById('modal-root');
function Modal({ children }) {
return ReactDOM.createPortal(
children,
modalRoot
);
}
Select your answer
Question 2/15
Should refs be used to expose component functions as a public API?
Select your answer
Question 3/15
What is required for a child functional component to handle a forwarded ref?
Select your answer
Question 4/15
What is the main difference between
React.createRef
and React.useRef
?
Select your answer
Question 5/15
How does React handle cleanup for refs during unmounting of the component?
Select your answer
Question 6/15
What is a common limitation of React portals?
Select your answer
Question 7/15
Can a React component be mounted into multiple portals simultaneously?
Select your answer
Question 8/15
What is a primary use case of React portals?
Select your answer
Question 9/15
Why might you use the
useImperativeHandle
hook with forwardRef
?
Select your answer
Question 10/15
How do portals handle propagation of parent CSS styles?
Select your answer
Question 11/15
What is a scenario where using refs is preferred over component state?
Select your answer
Question 12/15
When using
useImperativeHandle
, what is passed to the hook to expose methods to the parent component?
Select your answer
Question 13/15
Can a functional component use multiple refs for different purposes?
Select your answer
Question 14/15
Which DOM event does not automatically propagate through React portals?
Select your answer
Question 15/15
Which of the following statements about React refs is true?
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
How do you create a portal in a React application?
Available answers
In React, you can create a portal by using the
ReactDOM.createPortal
method, which takes two arguments: the JSX you want to render and the DOM element to render it into.
Question 2/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 3/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
What is required for a child functional component to handle a forwarded ref?
Available answers
A child component must be wrapped with
React.forwardRef
to handle refs forwarded from parent components and manage the refs correctly.
Question 4/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 5/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 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
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 8/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
What is a primary use case of React portals?
Available answers
React portals allow you to render a child component into a DOM node outside of the parent component's DOM hierarchy. This can be useful for creating modals or tooltips that need to overlay other parts of the UI.
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
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 11/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 12/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 13/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 14/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 15/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.