[英]React Native - import javascript file into another javascript file as text
I've got a MyFirstPage.js
file which is something like this: 我有一个
MyFirstPage.js
文件,它是这样的:
renderCards() {
let icon = this.state.icon;
....
....
....
}
Which i write to render my cards inside my main render function! 我写的用来在我的主要渲染功能中渲染卡的内容! The point is, because it's getting too long (about 350 lines), i want to create another file and import this
renderCards()
AS TEXT so i can i have it in the MyFirstPage.js
main render! 关键是,因为它变得太长(大约350行),我想创建另一个文件并以文本形式导入
renderCards()
这样我就可以在MyFirstPage.js
主渲染器中使用它了! Please note that let icon = this.state.icon;
请注意,
let icon = this.state.icon;
which refer to the MyFirstPage.js
state - so i have to import it AS TEXT . 它引用
MyFirstPage.js
状态-因此我必须将其作为TEXT导入。
I tried: 我试过了:
export default {
renderCards() {
....
....
....
}
}
but not working! 但不起作用!
Any idea? 任何想法?
Thanks in advance!
提前致谢!
You can define code in a string and execute it in Javascript. 您可以在字符串中定义代码并用Javascript执行。 We can use
eval()
to evaluate a String as a piece of code. 我们可以使用
eval()
将String评估为一段代码。 This approach has a security, reliability, and performance concerns because eval
can essentially inject code into your script, and that code is simply determined by what String is passed to it. 这种方法具有安全性,可靠性和性能方面的问题,因为
eval
实质上可以将代码注入脚本中,并且该代码仅由传递给它的String来确定。 That said, most Babel transpiling presets use strict mode, and this limits eval
to some extent by ultimately not allowing it to instantiate new variables in the surrounding scope. 就是说,大多数Babel转译预设都使用严格模式,并且最终不允许在实例范围内实例化新变量,从而在一定程度上限制了
eval
。 What it can still do, is define a function and evaluate that function and assign it to a variable in the external scope like so: 它仍然可以做的是定义一个函数并评估该函数并将其分配给外部作用域中的变量,如下所示:
var a = 5
var b = 10
var c = eval('function abc() { return a + b }; abc;')
assert(c() === 15)
With this ability in hand, we can wrap your entire 'renderCards' function definition in a String (by way of template literal): 有了此功能,我们可以将您的整个“ renderCards”函数定义包装在String中(通过模板文字):
export default `renderCards() {
let icon = this.state.icon;
....
....
....
}; renderCards;`
Import this string ( import renderCardsImport from {relative path to file}
in the react component MyFirstPage.js
, and in the constructor you need to do a few things: 导入此字符串(
import renderCardsImport from {relative path to file}
react组件MyFirstPage.js
中的import renderCardsImport from {relative path to file}
,在构造函数中,您需要做一些事情:
constructor(props){
super(props)
this.state = {
icon: // whatever this is
}
let renderCards = eval(renderCardsImport)
// so you have access to the parent class object this
this.renderCards = renderCards.bind(this)
}
And now you should be able to call renderCards()
in the render()
method or anyplace else in the component. 现在,您应该可以在
render()
方法或组件中的其他任何地方调用renderCards()
了。 Phewww. Phewww。 That was a doozy, but learned so much about eval, strict mode, etc. Hope it helps.
那真是个笨蛋,但对评估,严格模式等学到了很多东西。希望对您有所帮助。
EDIT: You can also use the Function
constructor it appears, as in var myFunct = Function('body of function with a return statement')
but you will need to remember to bind it to this
as well. 编辑:您还可以使用它出现的
Function
构造Function
,如var myFunct = Function('body of function with a return statement')
但您也需要记住将其绑定this
。 If you use Function, you avoid performance and security hit of eval. 如果使用功能,则可以避免性能和安全性受到影响。 Strict mode takes away most of the reliability problems.
严格模式消除了大多数可靠性问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.