将变量传递到React Native Webview



当Webview第一次使用react原生Webview加载时,我正试图将一些变量数据传递给它。然而,它似乎不像我使用模板文字的方式那样工作。

const arr = ["something"]
const injectedJS = `let webviewArr = ${arr}`
return (
<WebView
scalesPageToFit={false}
mixedContentMode="compatibility"
injectedJavaScript={injectedJS}
src={{ html: `some html here` }}
/>
)

为什么不起作用?如果我只是使用硬编码数据,它会很好地工作。但是,当我需要从其他地方导入数据并将其植入我的Webview时,这种方法只会破坏应用程序。

我发现双重字符串化(我不知道如何用其他词来形容它(可以正常工作。

let arr = ['"something"']

let str = "something"
const injectedJS = `let webviewArr = ['${str}']`

如果有更好的解决方案,请告诉我。

最新更新