0
0
React Nativemobile~20 mins

TextInput component in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TextInput Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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>
  );
}
AThe TextInput shows 'Hell' and the Text below also shows 'Hell'.
BThe TextInput shows 'Hello' but the Text below shows 'Hell'.
CThe TextInput and Text below both show 'Hello'.
DThe TextInput shows 'Hell' but the Text below shows 'Hello'.
Attempts:
2 left
💡 Hint
The TextInput's value is controlled by the state variable 'text'. When you delete a letter, the state updates accordingly.
📝 Syntax
intermediate
1: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?
A<TextInput placeholderText='Enter name' />
B<TextInput placeHolder='Enter name' />
C<TextInput placeholder='Enter name' />
D<TextInput placeholder-text='Enter name' />
Attempts:
2 left
💡 Hint
React Native uses camelCase props. Check the exact spelling of 'placeholder'.
lifecycle
advanced
1:30remaining
What happens when you set TextInput's editable prop to false?
If you set , what is the behavior of the input field?
AThe TextInput becomes read-only; user cannot type or edit text.
BThe TextInput disables only the placeholder text.
CThe TextInput clears its content automatically.
DThe TextInput hides the keyboard but still allows text editing.
Attempts:
2 left
💡 Hint
Think about what 'editable' means for a text input field.
navigation
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>
  );
}
AonPress={() => TextInput.blur()}
BonPress={() => Keyboard.dismiss()}
ConPress={() => setText('')}
DonPress={() => Keyboard.show()}
Attempts:
2 left
💡 Hint
There is a Keyboard API in React Native to control the keyboard.
🔧 Debug
expert
2: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>
  );
}
AThe TextInput is missing a style, so text is invisible.
BThe value prop is missing, so TextInput is uncontrolled.
CThe placeholder prop is incorrectly used, causing no text display.
DThe onChangeText handler resets text to empty string, so input never updates.
Attempts:
2 left
💡 Hint
Check what the onChangeText function does with the input value.