0
0
React Nativemobile~20 mins

TextInput with controlled state 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 when typing in this controlled TextInput?

Consider this React Native component with a controlled TextInput. What will the Text below display as you type?

React Native
import React, { useState } from 'react';
import { View, TextInput, 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 Text below shows the reversed input text.
BThe Text below remains empty no matter what you type.
CThe Text below shows exactly what you type in real time.
DThe Text below shows the input text but only after you press Enter.
Attempts:
2 left
💡 Hint

Think about how the value and onChangeText props work together in a controlled input.

📝 Syntax
intermediate
2:00remaining
Which option correctly updates state on TextInput change?

Which code snippet correctly updates the state when the user types in a controlled TextInput?

A<TextInput value={text} onChangeText={(text) => setText(text)} />
B<TextInput value={text} onChangeText={(e) => setText(e.target.value)} />
C<TextInput value={text} onChange={(e) => setText(e.nativeEvent.text)} />
D<TextInput value={text} onChangeText={setText} />
Attempts:
2 left
💡 Hint

Remember that onChangeText receives the new text string directly, not an event object.

lifecycle
advanced
2:00remaining
What happens if you omit the value prop in a controlled TextInput?

In a controlled TextInput, what is the effect of removing the value prop but keeping onChangeText?

React Native
import React, { useState } from 'react';
import { TextInput } from 'react-native';

export default function App() {
  const [text, setText] = useState('');
  return <TextInput onChangeText={setText} placeholder="Type here" />;
}
AThe TextInput becomes uncontrolled and user input is visible but state is updated.
BThe TextInput will not accept any input and appears disabled.
CThe app crashes with a runtime error about missing value prop.
DThe TextInput shows the last typed value but does not update state.
Attempts:
2 left
💡 Hint

Think about controlled vs uncontrolled inputs and what happens when value is missing.

🔧 Debug
advanced
2:00remaining
Why does this controlled TextInput not update the displayed text?

Given this code, why does the TextInput not show what the user types?

React Native
import React, { useState } from 'react';
import { TextInput } from 'react-native';

export default function App() {
  const [text, setText] = useState('');
  return <TextInput value={text} onChangeText={() => setText('')} placeholder="Type here" />;
}
ABecause <code>onChangeText</code> resets state to empty string on every change.
BBecause <code>value</code> is not set to the state variable.
CBecause <code>onChangeText</code> is missing the event parameter.
DBecause the placeholder text overrides the input value.
Attempts:
2 left
💡 Hint

Look at what onChangeText does to the state on every input.

🧠 Conceptual
expert
2:00remaining
What is the main benefit of using controlled TextInput components?

Why do developers prefer controlled TextInput components over uncontrolled ones in React Native?

AThey automatically improve app performance by reducing re-renders.
BThey prevent the user from typing invalid characters by default.
CThey enable the keyboard to show special keys automatically without extra code.
DThey allow the app to have full control over the input value and validation in real time.
Attempts:
2 left
💡 Hint

Think about how state and UI stay in sync with controlled inputs.