Functional Components are the modern and recommended way to create UI components in React Native. They are simple JavaScript functions that return JSX and can use React Hooks for state and lifecycle features.
useState and useEffectthis keywordA functional component is a normal JavaScript function that returns JSX. It can accept props as parameters.
// Basic functional component syntax
function MyComponent() {
return (
<Text>Hello World</Text>
);
}
// Functional component with props
import React from "react";
import { Text, View } from "react-native";
function Greeting(props) {
return (
<View>
<Text>Hello, {props.name}!</Text>
</View>
);
}
export default Greeting;
This component displays a greeting message using props. When you pass name="Meghraj", it will render:
Hello, Meghraj!
Below is the code for a Counter. Underneath the code block, try the Live Simulator to see how useState works in real-time!
// Interactive counter using useState hook
import React, { useState } from "react";
import { Text, Button, View } from "react-native";
function Counter() {
// Initialize state variable 'count' with 0
const [count, setCount] = useState(0);
return (
<View>
<Text>Count: {count}</Text>
<Button title="Increase" onPress={() => setCount(count + 1)} />
</View>
);
}
export default Counter;
useState to manage toggle state