Complete the code to create a controlled TextInput that updates the state on text change.
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=[1] placeholder="Type here" /> </View> ); }
text instead of the setter function.onChange instead of onChangeText.The onChangeText prop expects a function to update the state. Here, setText is the correct function to update the text state.
Complete the code to initialize the state with the string 'Hello'.
const [text, setText] = useState([1]);The initial state should be the string "Hello" to start the TextInput with that text.
Fix the error in the TextInput by completing the code to make it controlled.
<TextInput value=[1] onChangeText={setText} placeholder="Enter text" />
setText as the value.The value prop must be the state variable text to make the TextInput controlled.
Fill both blanks to create a controlled TextInput that updates the state and shows the current text below.
import React, { useState } from 'react'; import { TextInput, View, Text } from 'react-native'; export default function App() { const [text, setText] = useState(''); return ( <View> <TextInput value=[1] onChangeText=[2] placeholder="Type here" /> <Text>{text}</Text> </View> ); }
The value prop should be the state variable text, and onChangeText should be the setter function setText to update the state.
Fill all three blanks to create a controlled TextInput that updates the state, shows the text, and clears it on button press.
import React, { useState } from 'react'; import { TextInput, View, Text, Button } from 'react-native'; export default function App() { const [text, setText] = useState(''); return ( <View> <TextInput value=[1] onChangeText=[2] placeholder="Type here" /> <Text>{text}</Text> <Button title="Clear" onPress={() => [3] /> </View> ); }
The TextInput's value is the state text, onChangeText updates the state with setText, and the button clears the text by calling setText('').