0
0
React Nativemobile~20 mins

Responsive dimensions (Dimensions API) in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Responsive Dimensions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
How does Dimensions.get('window') behave on device rotation?
In React Native, when you use Dimensions.get('window') to get screen width and height, what happens if the user rotates the device from portrait to landscape?
AThe width and height values update only if you add an event listener for dimension changes.
BThe width and height values stay the same until the app is restarted.
CThe width and height values swap only if you call <code>Dimensions.get('screen')</code> instead.
DThe width and height values update automatically to reflect the new orientation.
Attempts:
2 left
💡 Hint
Think about whether Dimensions.get returns live values or snapshots.
🧠 Conceptual
intermediate
2:00remaining
What is the difference between 'window' and 'screen' in Dimensions API?
In React Native's Dimensions API, what is the key difference between Dimensions.get('window') and Dimensions.get('screen')?
A<code>'window'</code> includes the entire physical screen, <code>'screen'</code> excludes safe areas.
B<code>'window'</code> updates on rotation, <code>'screen'</code> never changes.
C<code>'window'</code> returns the app's layout size, <code>'screen'</code> returns the device's pixel density.
D<code>'window'</code> excludes status bar and navigation bar areas, <code>'screen'</code> includes them.
Attempts:
2 left
💡 Hint
Think about what parts of the screen the app can draw on.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly listens for dimension changes?
You want to update your React Native component when the screen size changes. Which code snippet correctly sets up and cleans up the listener using the Dimensions API?
React Native
import { useEffect } from 'react';
import { Dimensions } from 'react-native';

function MyComponent() {
  useEffect(() => {
    // Your code here
  }, []);
}
A
useEffect(() =&gt; {
  const subscription = Dimensions.addEventListener('change', ({ window }) =&gt; {
    console.log(window.width);
  });
  return () =&gt; subscription.remove();
}, []);
B
useEffect(() =&gt; {
  Dimensions.addEventListener('change', ({ window }) =&gt; {
    console.log(window.width);
  });
}, []);
C
useEffect(() =&gt; {
  const handler = ({ window }) =&gt; console.log(window.width);
  Dimensions.addEventListener('change', handler);
}, []);
D
useEffect(() =&gt; {
  const handler = ({ window }) =&gt; console.log(window.width);
  Dimensions.addEventListener('change', handler);
  return () =&gt; Dimensions.removeEventListener('change', handler);
}, []);
Attempts:
2 left
💡 Hint
Check how to properly remove listeners in modern React Native versions.
lifecycle
advanced
2:00remaining
Why might your component not re-render on dimension change?
You added a Dimensions change listener to update your component's state on screen rotation, but the UI does not update. What is the most likely reason?
ADimensions API does not support listening to changes on Android devices.
BYou used <code>Dimensions.get('screen')</code> instead of <code>'window'</code> in the listener.
CYou forgot to update the component's state inside the listener callback.
DYou need to call <code>forceUpdate()</code> manually after dimension changes.
Attempts:
2 left
💡 Hint
Think about React's rendering mechanism and state updates.
🔧 Debug
expert
2:00remaining
What error occurs with this Dimensions listener code?
Consider this React Native code snippet:
useEffect(() => {
  const handler = ({ window }) => {
    setWidth(window.width);
  };
  Dimensions.addEventListener('change', handler);
  return () => Dimensions.removeEventListener('change', handler);
}, []);
What error or problem will this code cause in React Native 0.65+?
AMemory leak because the listener is never removed.
BTypeError because removeEventListener is not a function on Dimensions.
CNo error; this is the correct way to add and remove listeners.
DSyntaxError due to incorrect arrow function usage.
Attempts:
2 left
💡 Hint
Check the updated API for adding/removing listeners in recent React Native versions.