React native 反应本机将动态内容加载到webview

React native 反应本机将动态内容加载到webview,react-native,webview,React Native,Webview,目前,我正在将一个html文件从我的android资产文件夹加载到webview中 dashboard.html文件包含一些脚本。 如果它是静态的,它工作正常 <WebView ref={(component) => (this.webview = component)} style={{ height: height * 1.3 }} javaScriptEnabled={true} domStorageEnabled={true} source={{uri:'

目前,我正在将一个html文件从我的android资产文件夹加载到webview中

dashboard.html文件包含一些脚本。 如果它是静态的,它工作正常

<WebView
  ref={(component) => (this.webview = component)}
  style={{ height: height * 1.3 }}
  javaScriptEnabled={true}
  domStorageEnabled={true}
  source={{uri:'file:///android_asset/graph/pages/dashboard.html'}}
  startInLoadingState={true}
  javaScriptEnabledAndroid={true}
  mixedContentMode={'compatibility'}
/>
(this.webview=component)}
样式={height:height*1.3}
javaScriptEnabled={true}
domStorageEnabled={true}
source={{uri:'file:///android_asset/graph/pages/dashboard.html'}}
startInLoadingState={true}
JavaScriptEnabledDroid={true}
mixedContentMode={'compatibility'}
/>
但我需要动态更新脚本中的变量。
如何从react native组件访问放置在assets文件夹中的脚本。

您无法访问和更新脚本文件,但在使用injectedJavaScript道具加载WebView后,您可以运行脚本或额外代码

例如:

const injectedJs = `
    alert("My custom code will go here!");
`;

return (
    <WebView
    ref={(component) => (this.webview = component)}
    style={{ height: height * 1.3 }}
    javaScriptEnabled={true}
    domStorageEnabled={true}
    source={{uri:'file:///android_asset/graph/pages/dashboard.html'}}
    startInLoadingState={true}
    javaScriptEnabledAndroid={true}
    mixedContentMode={'compatibility'}
    injectedJavaScript={injectedJs}
    javaScriptEnabledAndroid={true}
    />
);
const injectedJs=`
警报(“我的自定义代码将转到此处!”);
`;
返回(
(this.webview=component)}
样式={height:height*1.3}
javaScriptEnabled={true}
domStorageEnabled={true}
source={{uri:'file:///android_asset/graph/pages/dashboard.html'}}
startInLoadingState={true}
JavaScriptEnabledDroid={true}
mixedContentMode={'compatibility'}
injectedJavaScript={injectedJs}
JavaScriptEnabledDroid={true}
/>
);

您无法访问和更新脚本文件,但在使用injectedJavaScript道具加载WebView后,您可以运行脚本或额外代码

例如:

const injectedJs = `
    alert("My custom code will go here!");
`;

return (
    <WebView
    ref={(component) => (this.webview = component)}
    style={{ height: height * 1.3 }}
    javaScriptEnabled={true}
    domStorageEnabled={true}
    source={{uri:'file:///android_asset/graph/pages/dashboard.html'}}
    startInLoadingState={true}
    javaScriptEnabledAndroid={true}
    mixedContentMode={'compatibility'}
    injectedJavaScript={injectedJs}
    javaScriptEnabledAndroid={true}
    />
);
const injectedJs=`
警报(“我的自定义代码将转到此处!”);
`;
返回(
(this.webview=component)}
样式={height:height*1.3}
javaScriptEnabled={true}
domStorageEnabled={true}
source={{uri:'file:///android_asset/graph/pages/dashboard.html'}}
startInLoadingState={true}
JavaScriptEnabledDroid={true}
mixedContentMode={'compatibility'}
injectedJavaScript={injectedJs}
JavaScriptEnabledDroid={true}
/>
);
您可以使用forceUpdate(); e、 g


onTestBtnClicked(){
常量html='test1';
this.setState({html});
这个.forceUpdate();
}
您可以使用forceUpdate(); e、 g


onTestBtnClicked(){
常量html='test1';
this.setState({html});
这个.forceUpdate();
}

谢谢,但我需要在加载webview之前更新脚本。除了将文件放入assets文件夹之外,是否还有其他方法将本地文件加载到webview中。也许您可以使用injectJavaScript函数,该函数允许您在调用脚本的准确时间运行脚本。您可以创建一个类似alertMessage=()=>{}的函数,并在webview的onLoadStart事件上调用上述函数。如果您想对脚本进行更改,我建议您使用上面提到的两种方法之一进行注入。谢谢,但我需要在加载webview之前更新脚本。除了将文件放入assets文件夹之外,是否还有其他方法将本地文件加载到webview中。也许您可以使用injectJavaScript函数,该函数允许您在调用脚本的准确时间运行脚本。您可以创建一个类似alertMessage=()=>{}的函数,并在webview的onLoadStart事件上调用上述函数。如果您想对脚本进行更改,我建议您使用上面提到的两种方法之一注入脚本。