當我插入條件時反應 webview 后處理程式作業正常,如果 url 匹配顯示警報在下面的代碼中不起作用
const [canGoBack, setCanGoBack] = useState(false);
const [canGoForward, setCanGoForward] = useState(false);
const [currentUrl, setCurrentUrl] = useState('');
const onPressHardwareBackButton = () => {
if (webview.current) {
webview.current.goBack();
return true;
} else {
return false;
}
};
useEffect(() => {
BackHandler.addEventListener('hardwareBackPress', onPressHardwareBackButton);
return () => {
BackHandler.removeEventListener('hardwareBackPress', onPressHardwareBackButton);
}
}, []);
<WebView
source={{ uri: 'https://example.com/' }}
ref={webview}
onNavigationStateChange={(navState) => {
setCanGoBack(navState.canGoBack);
setCanGoForward(navState.canGoForward);
setCurrentUrl(navState.url);
}}
/>
如果像www.example.com/dashboardCurrentUrl這樣的匹配需要提醒
Alert.alert("Hold on!", "Are you sure you want to go back?", [
{
text: "Cancel",
onPress: () => null,
style: "cancel"
},
{ text: "YES", onPress: () => BackHandler.exitApp() }
])
怎么能做到這一點
uj5u.com熱心網友回復:
您已經將當前 url 保存為 state,因此只需監聽currentUrl更改就足夠了。
useEffect(() => {
if(currentUrl === 'www.example.com/dashboard') {
Alert.alert("Hold on!", "Are you sure you want to go back?", [
{
text: "Cancel",
onPress: () => null,
style: "cancel"
},
{ text: "YES", onPress: () => BackHandler.exitApp() }
]);
}
}, [currentUrl]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/525767.html
標籤:反应式网页浏览
