webview - 使用WebView更改默认页面时如何进行条件渲染(TouchableOpacity)?
问题描述
使用 WebView 更改默认页面时如何进行条件渲染?我想呈现一个 TouchableOpacity 组件以作为后退按钮呈现给用户,只有在用户单击第二个页面的链接并返回到上一页之后。
My NavigationView.js
import * as React from 'react';
import { View, StyleSheet, TouchableOpacity,
Animated } from 'react-native';
import { AntDesign} from "@expo/vector-icons";
const NavigationView = ({ onBackPress }) => (
<View style={[styles.container2]}>
<TouchableOpacity style={[ styles.container2, styles.button, styles.menu]} onPress={onBackPress}>
<Animated.View>
<AntDesign name="back"size={28} color="#fff"/>
</Animated.View>
</TouchableOpacity>
</View>
);
const styles = StyleSheet.create({
container2: {
alignItems: 'flex-start',
justifyContent: 'flex-end',
marginHorizontal:3,
marginVertical: 2,
},
title: {
fontSize: 20,
fontWeight: 'bold',
},
separator: {
marginVertical: 30,
height: 1,
width: '80%',
},
button: {
position: "absolute",
width: 55,
height: 55,
borderRadius: 55 / 2,
alignItems: "center",
justifyContent: "center",
shadowRadius: 10,
shadowColor: "#2f95dc",
shadowOpacity: 0.3,
shadowOffset: { height: 10}
},
menu : {
backgroundColor: "#2f95dc",
}
});
export default NavigationView;
如何使用 webview 使后退按钮仅在用户走到下一页后才出现。当用户返回到初始 webview 中呈现的页面时,该按钮将再次隐藏。
My TabAtualidadesScreen.js
import * as React from 'react';
import { WebView } from 'react-native-webview';
import { useState, useEffect, useRef, Component} from 'react';
import {Text, View } from '../components/Themed';
import * as Progress from 'react-native-progress';
import NavigationView from '../navigation/NavigationView';
import { isLoading } from 'expo-font';
const TabAtualidadesScreen = () => {
const [progress, setProgress] = useState(0);
const [isLoaded, setLoaded] = useState(false);
const webViewRef = useRef();
const [canGoBack, setCanGoBack] = useState(false);
const handleBackPress = () => {
webViewRef.current.goBack();
}
return <>
{
!isLoaded ?
<Progress.Bar
borderWidth={0}s
borderRadius={0}
color='#2778a3'
progress={progress}
width={null}
/>
: null
}
<WebView
ref={webViewRef}
source={{ uri: 'yourInitialUrl' }}
onLoadEnd={() => setLoaded(true) }
onLoadProgress={({nativeEvent}) => setProgress(nativeEvent.progress)}
onNavigationStateChange={(state) => {
const back = state.canGoBack;
setCanGoBack(back);
setWebViewUrlChanged = webviewState => {
if (webviewState.url !== yourInitialUrl) {
this.setState({ isWebViewUrlChanged: true });
{<NavigationView onBackPress={handleBackPress}/>}
}
};
}}
/>
</>
}
export default TabAtualidadesScreen;
使用 WebView 更改默认页面时如何进行条件渲染?我想呈现一个 TouchableOpacity 组件以作为后退按钮呈现给用户,只有在用户单击第二个页面的链接并返回到上一页之后。
解决方案
推荐阅读
- python - 如何按单词分组并创建由浮点值组成的等效列?(熊猫)
- javascript - 方法链可以像 Javascript 中内置函数的实现方式那样实现吗?
- javascript - 基于屏幕调整大小的香草javascript函数
- reactjs - 是否可以将从 API 获得的城市传递给以下函数?
- mongodb - mongo中的地理空间半径查询错误
- c# - 如何正确实现 Outlook 检查器的 Wrapper 类
- javascript - 如何使用 image-js 和 JavaScript 标准化图像(直方图拉伸)?
- jupyter-notebook - 如何执行不同大小的相关性
- database - 多次加入日期维度?- Kimball 关于数据仓库和维度建模的书
- flutter - 自动选择圆形列表颤振中的项目