简体   繁体   English

将动态变量注入React Native的WebView的injectJavaScript字符串

[英]Inject dynamic vars into React Native's WebView's injectedJavaScript string

I'm loading this WebView in my app: 我正在我的应用程序中加载此WebView:

           <WebView
                style={{
                    marginTop: 5,
                    flex:1,
                    flexDirection: 'column',
                    alignItems:'center',
                    justifyContent: 'center',
                    backgroundColor: 'rgba(255,255,255,0.8)',
                    height:300
                  }}
                source={require('./test.html')}
                injectedJavaScript={injectedScript}
                scalesPageToFit={true}
            />

Since I want this html file to display a string that I got just before the render was called - I want to do the following: 因为我希望这个html文件显示我在调用render之前得到的字符串 - 我想要执行以下操作:

        let injectedScript = `CKEDITOR.instances.editor.setData(${bodyForDisplay});`;

But it just doesn't work. 但它只是不起作用。 When I replace the ${bodyForDisplay} with a static string (ie "test") - it works flawlessly. 当我用静态字符串(即“测试”)替换$ {bodyForDisplay}时 - 它完美无缺。

Any ideas on how to inject a dynamic value here? 关于如何在这里注入动态值的任何想法?

You need to surround ${bodyForDisplay} with a single or double quotes, as it's a string parameter to the method setData . 你需要用单引号或双引号括${bodyForDisplay} ,因为它是方法setData的字符串参数。

Also, make sure to escape the string inside bodyForDisplay so it won't close the opening string quote (single or double). 此外,请确保转义bodyForDisplay内的字符串,以便它不会关闭开始字符串引号(单引号或双引号)。 It's never safe to trust user input so you should properly escape other chars in that string as well. 信任用户输入永远不安全,所以你也应该正确地逃避该字符串中的其他字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM