Challenge - 5 Problems
TextInput Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this TextInput behavior?
Consider this React Native code snippet. What will the TextInput display after typing 'Hello' and then deleting the last letter?
React Native
import React, { useState } from 'react'; import { TextInput, View, Text } from 'react-native'; export default function App() { const [text, setText] = useState(''); return ( <View> <TextInput value={text} onChangeText={setText} placeholder="Type here" /> <Text>{text}</Text> </View> ); }
Attempts:
2 left
💡 Hint
The TextInput's value is controlled by the state variable 'text'. When you delete a letter, the state updates accordingly.
✗ Incorrect
The TextInput is controlled by the 'text' state. When you delete the last letter, the state updates to 'Hell', so both the input and the Text component show 'Hell'.
📝 Syntax
intermediate1:30remaining
Which option correctly sets a placeholder in TextInput?
You want to show a placeholder text 'Enter name' inside a TextInput. Which code snippet does this correctly?
Attempts:
2 left
💡 Hint
React Native uses camelCase props. Check the exact spelling of 'placeholder'.
✗ Incorrect
The correct prop name is 'placeholder' all lowercase. Other variants like 'placeHolder' or 'placeholderText' are invalid and won't work.
❓ lifecycle
advanced1:30remaining
What happens when you set TextInput's editable prop to false?
If you set , what is the behavior of the input field?
Attempts:
2 left
💡 Hint
Think about what 'editable' means for a text input field.
✗ Incorrect
Setting editable to false disables user interaction with the TextInput, making it read-only.
advanced
2:00remaining
How to dismiss the keyboard when pressing a button after typing in TextInput?
You have a TextInput and a Button. After typing, you want the keyboard to hide when the button is pressed. Which code snippet achieves this?
React Native
import React, { useState } from 'react'; import { TextInput, Button, View, Keyboard } from 'react-native'; export default function App() { const [text, setText] = useState(''); return ( <View> <TextInput value={text} onChangeText={setText} /> <Button title="Submit" onPress={() => { /* What to do here? */ }} /> </View> ); }
Attempts:
2 left
💡 Hint
There is a Keyboard API in React Native to control the keyboard.
✗ Incorrect
Keyboard.dismiss() hides the keyboard. Calling it inside onPress will close the keyboard when the button is pressed.
🔧 Debug
expert2:30remaining
Why does this TextInput not update the displayed text?
Look at this code. The user types but the TextInput does not show the typed text. What is the cause?
React Native
import React, { useState } from 'react'; import { TextInput, View } from 'react-native'; export default function App() { const [text, setText] = useState(''); return ( <View> <TextInput value={text} onChangeText={() => setText('')} placeholder="Type here" /> </View> ); }
Attempts:
2 left
💡 Hint
Check what the onChangeText function does with the input value.
✗ Incorrect
The onChangeText callback ignores the input and always sets text to empty string, so the input never shows typed characters.