Variables: Store data that can change but won't trigger a re-render when modified. You need to manually handle re-renders if needed.
State: Special type of variable managed by React that triggers a re-render when updated. Use the useState
hook to manage state in functional components.
import React, { useState } from 'react';
import { View, Text, Button } from 'react-native';
const App = () => {
const [count, setCount] = useState(0);
const incrementCount = () => {
setCount(count + 1);
};
return (
<View>
<Text>Count: {count}</Text>
<Button title="Increment" onPress={incrementCount} />
</View>
);
};
export default App;
© www.thecoderjob.com. All Rights Reserved. Designed by HTML Codex