React Native

How do you handle state in React Native?

Medium
3
Added
In React Native, you can handle state using the useState hook for functional components or the state property in class components.

Solution Code

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

const App = () => {
  const [count, setCount] = useState(0);

  return (
    <View>
      <Text>Count: {count}</Text>
      <Button title="Increment" onPress={() => setCount(count + 1)} />
    </View>
  );
};

export default App;
Explanation
This code demonstrates how to use the useState hook to manage state in a functional component.

Guided Hints

{'hint': 'Use the useState hook for functional components.'}
{'hint': 'Use the state property for class components.'}