usafeft将在react Native中自动webview中的更新代码赢得更新代码
我使用ReactNavigation的堆栈导航。打开屏幕时,XI希望自动更新“ LET USERANSWER =”“;”。在WebView中使用字符串。但是,代码不会运行。当我将文件保存在Visual Studio中时,它确实运行并更新了变量。
我通过使用效率使代码运行。这是我的WebViewComponent中的WebView:
import { useState, useContext, useEffect } from "react";
import { StyleSheet, Text, View, Pressable } from "react-native";
import { WebView } from "react-native-webview";
import { FontAwesome } from "@expo/vector-icons";
import DarkModeContext from "../store/darkmode";
const html = `
<!DOCTYPE html>
<html>
...
<script>
function update() {
var idoc = document.getElementById('iframe').contentWindow.document;
idoc.open();
idoc.write(editor.getValue());
idoc.close();
}
let userAnswer = "";
以下是应该在WebViewComponent中运行的使用效率:
useEffect(() => {
webref.injectJavaScript(props.defaultContent);
console.log("mounted")
},[]);
return (
<View>
<View style={{ width: "100%", height: "65%", marginTop: 0 }}>
<WebView
ref={(r) => (webref = r)}
androidHardwareAccelerationDisabled={true} // To prevent crash when opening screen
scalesPageToFit={false}
scrollEnabled={false}
source={{ html }}
/>
</View>
我从屏幕上获得默认的内容prop,我正在使用wwebviewcomponent在:
<WebViewComponent
defaultContent = {`userAnswer += "Hello World"
editor.setValue(userAnswer, 1);
`}
/>
它的作用:它的作用是:它的作用是:它的作用是:它的作用是:它的作用是:它的作用在:它的作用是好像组件在通过它在通过它导航时通过它上市反应措施,尽管情况当然不是这样。 Console.Log确实返回“已安装”。有什么想法吗?
谢谢!
I use a stack navigation from reactnavigation. When opening Screen X I want to automatically update "let userAnswer = "";" with a string within a webview. However, the code won't run. It does run and updates the variable when I save my file in visual studio.
I make the code run via useEffect. This is the webview in my WebViewComponent:
import { useState, useContext, useEffect } from "react";
import { StyleSheet, Text, View, Pressable } from "react-native";
import { WebView } from "react-native-webview";
import { FontAwesome } from "@expo/vector-icons";
import DarkModeContext from "../store/darkmode";
const html = `
<!DOCTYPE html>
<html>
...
<script>
function update() {
var idoc = document.getElementById('iframe').contentWindow.document;
idoc.open();
idoc.write(editor.getValue());
idoc.close();
}
let userAnswer = "";
Here is the useEffect that is supposed to run in WebViewComponent:
useEffect(() => {
webref.injectJavaScript(props.defaultContent);
console.log("mounted")
},[]);
return (
<View>
<View style={{ width: "100%", height: "65%", marginTop: 0 }}>
<WebView
ref={(r) => (webref = r)}
androidHardwareAccelerationDisabled={true} // To prevent crash when opening screen
scalesPageToFit={false}
scrollEnabled={false}
source={{ html }}
/>
</View>
I get default content prop from the screen I am using the wWebViewComponent in:
<WebViewComponent
defaultContent = {`userAnswer += "Hello World"
editor.setValue(userAnswer, 1);
`}
/>
It acts as if the component doesn't mount when navigating to it via reactnavigation, though this is of course not the case. The console.log does return "mounted". Any ideas?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论