深入了解React Native的状态管理
import React, { useState } from 'react';
import { Text, View, Button } from 'react-native';
const CounterComponent = () => {
const [count, setCount] = useState(0);
const increment = () => {
setCount(count + 1);
};
const decrement = () => {
setCount(count - 1);
};
return (
<View>
<Text>Count: {count}</Text>
<Button onPress={increment} title="Increment" />
<Button onPress={decrement} title="Decrement" />
</View>
);
};
export default CounterComponent;
这段代码展示了如何使用React Native和Hooks API来创建一个简单的计数器组件。useState
Hook用于添加状态到函数组件,increment
和decrement
函数用于更新计数器值。这个例子简单易懂,并且有效地展示了React Native状态管理的基本方法。
评论已关闭