当我单击标题为“增加”的按钮时,我希望计数增加并反映更改。状态变量“计数”在点击时不会增加。我想念什么?
App文件如下:
App.tsx
<OpenIcon onClick={toggle}>open sidebar</OpenIcon>
上下文文件如下所示: ./stores/CounterStore.tsx
import { StatusBar } from 'expo-status-bar';
import React, { useContext, useState }from 'react';
import { Button, StyleSheet, Text, View } from 'react-native';
import { observer } from 'mobx-react-lite';
import { CounterStoreContext } from './stores/CounterStore';
const App = observer(() => {
const counterStore = useContext(CounterStoreContext);
return (
<View style={styles.container}>
<Text>Heyyy</Text>
{console.log(counterStore.count)}
<Text> {counterStore.count}</Text>
<StatusBar style="auto" />
<Button title='increment' onPress={() => counterStore.count++}/>
</View>
);
});
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
export default App;