我的 React Native 代码有一个包含选择器的覆盖/模式。当我打开叠加层并从选择器中选择一个选项时,叠加层会关闭,而不是保持打开状态。我注意到它与选择选项时调用 setState() 有关。我怎样才能防止它关闭?How can I update the variable when the option is chosen without the modal closing.
这是我的代码的一部分。如果我做错了什么,请告诉我。
import {Picker} from '@react-native-picker/picker';
import { Overlay, Button, Input } from 'react-native-elements';
import { StyleSheet, View, Text, Switch } from "react-native";
...
const [packageStatus, setPackageStatus] = useState();
const [statusModalVisible, setStatusModalVisible] = useState(false);
// The modal
const statusModal = () =>{
return (
<Overlay transparent={true} animationType={"fade"}
isVisible={statusModalVisible}
onBackdropPress={()=>setStatusModalVisible(false)}>
<View style={{alignItems:'center'}}>
<Text style={styles.h4}>Update the status</Text>
<Picker
style={{width:350}}
selectedValue={packageStatus}
onValueChange={(itemValue, itemIndex) =>
{setPackageStatus(itemValue);}
}>
<Picker.Item label="Awaiting pickup" value="Awaiting pickup" />
<Picker.Item label="Picked up" value="Picked up" />
<Picker.Item label="Left the country" value="Left the country" />
</Picker>
<Button title="Update" type='clear' style={{color: colors.primary}}
onPress={() => setStatusModalVisible(false)}></Button>
</View>
</Overlay>
)
}
激活模态的按钮
<Button title='Change' type='clear' onPress={() => {setModalStatus('status', true)}}></Button>