Challenge - 5 Problems
KeyboardAvoidingView Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
KeyboardAvoidingView behavior on iOS
What will happen when the keyboard appears in this React Native code using KeyboardAvoidingView with behavior set to 'padding' on iOS?
React Native
import React from 'react'; import { KeyboardAvoidingView, TextInput, Platform } from 'react-native'; export default function App() { return ( <KeyboardAvoidingView behavior={Platform.OS === 'ios' ? 'padding' : 'height'} style={{flex: 1}}> <TextInput placeholder="Type here" style={{marginTop: 600, height: 40, borderColor: 'gray', borderWidth: 1}} /> </KeyboardAvoidingView> ); }
Attempts:
2 left
💡 Hint
Think about how 'padding' behavior adjusts the view when the keyboard shows on iOS.
✗ Incorrect
On iOS, KeyboardAvoidingView with behavior 'padding' adds padding to the bottom of the view equal to the keyboard height, moving the TextInput up so it remains visible.
❓ ui_behavior
intermediate2:00remaining
KeyboardAvoidingView behavior on Android
In this React Native code, what is the effect of setting KeyboardAvoidingView behavior to 'height' on Android when the keyboard appears?
React Native
import React from 'react'; import { KeyboardAvoidingView, TextInput, Platform } from 'react-native'; export default function App() { return ( <KeyboardAvoidingView behavior={Platform.OS === 'android' ? 'height' : 'padding'} style={{flex: 1}}> <TextInput placeholder="Enter text" style={{marginTop: 600, height: 40, borderColor: 'gray', borderWidth: 1}} /> </KeyboardAvoidingView> ); }
Attempts:
2 left
💡 Hint
Consider how 'height' behavior changes the container size on Android.
✗ Incorrect
On Android, 'height' behavior reduces the height of KeyboardAvoidingView by the keyboard height, effectively moving content up to avoid being hidden.
❓ lifecycle
advanced2:00remaining
KeyboardAvoidingView and keyboard dismissal
What happens if you wrap your screen in KeyboardAvoidingView but forget to set keyboardVerticalOffset on iOS when you have a fixed header of 80 pixels?
React Native
import React from 'react'; import { KeyboardAvoidingView, TextInput, Platform } from 'react-native'; export default function App() { return ( <KeyboardAvoidingView behavior={Platform.OS === 'ios' ? 'padding' : 'height'} style={{flex: 1}}> <TextInput placeholder="Input" style={{marginTop: 600, height: 40, borderColor: 'gray', borderWidth: 1}} /> </KeyboardAvoidingView> ); }
Attempts:
2 left
💡 Hint
Think about how keyboardVerticalOffset adjusts for fixed elements like headers.
✗ Incorrect
Without keyboardVerticalOffset, KeyboardAvoidingView does not account for fixed headers, so the TextInput may be hidden behind the header when the keyboard appears.
📝 Syntax
advanced2:00remaining
Correct usage of KeyboardAvoidingView props
Which of the following KeyboardAvoidingView usages will cause a syntax error?
Attempts:
2 left
💡 Hint
Check how string props must be passed in JSX.
✗ Incorrect
In JSX, string props must be quoted. behavior=padding without quotes causes a syntax error.
🔧 Debug
expert2:00remaining
Unexpected KeyboardAvoidingView behavior on Android
You notice that on Android, KeyboardAvoidingView with behavior='padding' does not move the TextInput up when the keyboard appears. What is the most likely cause?
Attempts:
2 left
💡 Hint
Check platform support for KeyboardAvoidingView behaviors.
✗ Incorrect
On Android, 'padding' behavior is not supported by KeyboardAvoidingView, so it does not move content up. 'height' or 'position' should be used instead.