如何添加<脚本>使用 ReactJS 标记? [英] How to add <script> tag using ReactJS?

查看:56
本文介绍了如何添加<脚本>使用 ReactJS 标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要声明一个 javascript 函数,如下所述:

render: function() {返回 (<div><SomeReactClass somefunction="myFunction"><脚本>函数 myFunction(索引,行){回报指数;<<<<此行中的错误}

);}

但是,它不会编译:解析错误:第 113 行:意外的令牌返回"

如何使用 ReactJS 添加标签?

<小时>

更新

我正在尝试使用 bootstrap-table 详细视图.该函数作为参数传递给网格,用于呈现行的详细信息.另请参阅示例源代码以便更好地理解.

当我按照你说的方式尝试时,它可以编译,但根本不起作用:

这是它的样子(在上面的例子中):

这就是我得到的

解决方案

我知道这是旧的,但我最近偶然发现了这个,这是我找到的最佳解决方案(我将它用于浏览器 polyfills,但它有效对于任何代码):

render: function() {返回 (<div><SomeReactClass somefunction="myFunction"><脚本危险地SetInnerHTML={{ __html:`function myFunction(index, row) {return index;}`}}/>

);}

I need to declare a javascript function as described below:

render: function() {
  return (
          <div>
            <SomeReactClass somefunction="myFunction">
            <script>
              function myFunction(index, row) {
                return index;                       <<<< error in this line 
              }
            </script>
          </div>
          );
}

But, it does not compile: "Parse Error: Line 113: Unexpected token return"

How can I add tag using ReactJS?


UPDATE

I'm trying to use bootstrap-table detail view. The function is passed as parameter to the grid and it is used to render the row's detail. Also, see the example's source code for a better understanding.

When I try the way you're saying, it compiles, but does not work at all:

This is how it looks like (in example above):

This is what I'm getting with <SomeReactClass somefunction={myFunction}>

解决方案

I know this is old, but i stumbled upon this recently and here is the best solution i found (i'm using it for browser polyfills, but it works for any code):

render: function() {
  return (
     <div>
        <SomeReactClass somefunction="myFunction">
        <script
          dangerouslySetInnerHTML={{ __html:
            `function myFunction(index, row) {return index;}`
          }}
        />
     </div>
  );
}

这篇关于如何添加&lt;脚本&gt;使用 ReactJS 标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆