带有 JSX 语法的 `eval()`ing 代码 [英] `eval()`ing code with JSX syntax in it

查看:86
本文介绍了带有 JSX 语法的 `eval()`ing 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,其中服务器正在生成一些 javascript 并通过 ajax 将其发送到客户端.问题是,我想在页面上使用 React 但不知道要调用哪个函数.现在 javascript 在 jQuery 中,我使用 eval() 在客户端执行 javascript.什么是适用于 JSX 的 eval() 的 React 等价物.

I have a website where the server is generating some javascript and sending it via ajax to the client. The problem is, I want to use React on the pages but don't know which function to call. Right now the javascript is in jQuery and I use eval() to execute the javascript on the client side. What is the React equivalent of eval() that works for JSX.

推荐答案

如果你想让它与 JSX 一起工作,你可以使用类似 Babel.

If you want it to work with JSX, you can transpile the code with something like Babel before you execute it.

例如,当使用 浏览器版本 Babel 时:

For example, when using the browser version of Babel:

var jsCode = babel.transform(jsxCode);
eval(jsCode.code);

还有一个 run 方法可以用来简单地执行代码:

There's also a run method you can use to simply execute the code:

babel.run(code);

transformrun 都采用可选的 options 哈希值;查看文档了解更多详情.

Both transform and run take an optional options hash; check the documentation for more details.

当然,按照标准,小心eval.

Of course, as is standard, be careful with eval.

这篇关于带有 JSX 语法的 `eval()`ing 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆