react-native - 再次渲染 react-native 应用程序 Web-View
问题描述
我有一个带有 webview 的 react-native 应用程序。当我单击通知时,我想转到预定义页面(导航到页面),但应用程序不呈现新视图。这是我打开应用程序时的代码(没有通知点击):
return (
<WebView
automaticallyAdjustContentInsets={false}
source={{
uri: 'https://my-url/',
}}
javaScriptEnabled={true}
domStorageEnabled={true}
startInLoadingState={true}
style={{
marginTop: 25,
}}
/>
);
而且,当我点击通知时:
return (
<WebView
automaticallyAdjustContentInsets={false}
source={{
uri: 'https://my-url/new',
}}
javaScriptEnabled={true}
domStorageEnabled={true}
startInLoadingState={true}
style={{
marginTop: 25,
}}
/>
);
当我点击通知时,应用程序再次在 https://my-url 中打开。
单击通知时,我想在 https://my-url/new 上导航。
你能帮我么?
解决方案
使用 state 设置 webview 的 url。当状态改变时,webview 将使用更新的 url 重新加载。
使用“https://my-url/”初始化 url 状态
假设您正在使用类组件,请在构造函数中设置初始 url。
constructor(props) {
super(props);
this.state = {
url: 'https://my-url/'
}
}
当您单击通知时,使用所需的 url 设置状态。
onNotification: function(notification) {
this.setState({
url: 'https://my-url/new'
})
}
这应该在 webview 中呈现预定义的页面
return (
<WebView
automaticallyAdjustContentInsets={false}
source={{
uri: this.state.url,
}}
javaScriptEnabled={true}
domStorageEnabled={true}
startInLoadingState={true}
style={{
marginTop: 25,
}}
/>
);
推荐阅读
- scala - curried 函数返回的非数值类型
- c# - 从文件夹中获取带有标签的预制游戏对象
- r - 如何使用 R 通过多个标准从不同数据框中分配值
- javascript - 如何使用数字键还原 JSON 对象
- javascript - 刷新Flask Web应用程序页面时提交先前选择的HTML表单
- c# - NetworkCredential(address, pass) 返回 null
- python-3.x - googleapiclient.errors.HttpError - 在 python3 但不是 python2 中返回“字段 [名称] 有问题 [无效项目资源名称]”
- eclipse - 如何使用 Eclipse IDE 调试基于 JavaFX 11 的 Maven 项目 (2019-03)
- python - 使用条件在已排序的 MultiIndex DataFrame 的第一行中设置列值
- java - 从其他活动中读取 SharedPreferences