【发布时间】:2020-08-13 03:04:33
【问题描述】:
我正在使用 Modal 在我的应用中打开条款。在这里,我可以毫无问题地打开和关闭模式。
但是,如果我打开模式一次,然后在应用程序内不执行任何操作,它就会进入睡眠模式。然后在手机上,它会显示应用程序,我看不到模态窗口,如果我再次尝试打开模态窗口它没有响应?
任何变通解决方案?
【问题讨论】:
标签: react-native react-native-modal
我正在使用 Modal 在我的应用中打开条款。在这里,我可以毫无问题地打开和关闭模式。
但是,如果我打开模式一次,然后在应用程序内不执行任何操作,它就会进入睡眠模式。然后在手机上,它会显示应用程序,我看不到模态窗口,如果我再次尝试打开模态窗口它没有响应?
任何变通解决方案?
【问题讨论】:
标签: react-native react-native-modal
有一个想法,你可以尝试使用Appstate来处理。
检测到app进入前台,然后关闭Modal,应该可以吧?
这是文档上的appstate(功能)示例,在控制台行关闭模态。(如果您使用类组件,与document类示例中使用的理论相同)
import React, { useEffect, useState } from "react";
import { AppState, StyleSheet, Text, View } from "react-native";
const AppStateExample = () => {
const [appState, setAppState] = useState(AppState.currentState);
useEffect(() => {
AppState.addEventListener("change", _handleAppStateChange);
return () => {
AppState.removeEventListener("change", _handleAppStateChange);
};
}, []);
const _handleAppStateChange = nextAppState => {
if (appState.match(/inactive|background/) && nextAppState === "active") {
console.log("App has come to the foreground!"); // Close the Modal here
}
setAppState(nextAppState);
};
return (
<View style={styles.container}>
<Text>Current state is: {appState}</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center"
}
});
export default AppStateExample;
试试看。
【讨论】: