React Native

How do you handle form inputs in React Native?

Medium
32
Added
You can handle form inputs in React Native using the useState hook to manage the state of the input fields.

Solution Code

React Native
-- Example Code --
import React, { useState } from "react";
import { View, TextInput, Button, Text } from "react-native";

const App = () => {
  const [text, setText] = useState(" ");

  return (
    <View>
      <TextInput
        style={{ height: 40, borderColor: "gray", borderWidth: 1 }}
        onChangeText={text => setText(text)}
        value={text}
      />
      <Button title="Submit" onPress={() => alert(text)} />
    </View>
  );
};

export default App;
Explanation
This code demonstrates how to handle form inputs using the useState hook.

Guided Hints

{'hint': 'Use the useState hook to manage input state.'}
{'hint': 'Use onChangeText to update the state.'}