简体   繁体   English

如何在脚本标签中启动var?

[英]How to initiate var in script tag?

This is the first website I'm building and an API I want to use requires me that I create a variable inside a script like so.这是我正在构建的第一个网站,我想使用的 API 要求我在这样的脚本中创建一个变量。

<script>
var someVariable = {
  api_key: "api_key_here",
  debug: true
};
</script>

When I place it inside my index.html, the file compiles and the API succeeds.当我将它放在我的 index.html 中时,文件会编译并且 API 成功。 However, if I try to place that same snippet of code inside my render method to React, I get:但是,如果我尝试将相同的代码片段放入 React 的渲染方法中,我会得到:

"} expected" after the "api_key"在“api_key”之后的“}预期”

I've tried to dangerouslySetInnerHTML to run the code and some other hacks but even if it does compile, on runtime I receive an error like this.我试图dangerouslySetInnerHTML地SetInnerHTML 来运行代码和其他一些黑客,但即使它确实可以编译,在运行时我也会收到这样的错误。

Objects are not valid as a React child (found: object with keys {api_key, debug}).对象作为 React 子对象无效(找到:带有键 {api_key, debug} 的对象)。 If you meant to render a collection of children...如果您打算渲染一组儿童...

I've been stuck on this for the longest time and can't find a solution on the internet.我已经被困在这个问题上很长时间了,无法在互联网上找到解决方案。 I would really appreciate it if anyone can help point me in the right direction.如果有人能帮助我指明正确的方向,我将不胜感激。 Thank you.谢谢你。

Full Render Method完整渲染方法

render() {
    return (
      <div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
  var pollfishConfig = {
    api_key: "api_key_goes_here",
    debug: true
  };
</script>
<script src="https://storage.googleapis.com/pollfish_production/sdk/webplugin/pollfish.min.js"></script>
      </div>
    )}

EDIT: I was able to get it to work using componentDidMount()编辑:我能够使用 componentDidMount() 让它工作

  componentDidMount() {
    const s = document.createElement('script');
    s.type = 'text/javascript';
    s.async = true;
    s.innerHTML = "var pollfishConfig = {api_key: \"api_key_here\", debug: true,
closeCallback: customSurveyClosed}; function customSurveyClosed(){console.log("test");}";
    document.body.appendChild(s);
  }

Is there a way to make the function customSurveyClosed access the react class where the componentDidMount is called?有没有办法让函数 customSurveyClosed 访问调用 componentDidMount 的反应类? For example, inside customSurveyClosed() change the state of the react component.例如,在 customSurveyClosed() 内部更改反应组件的状态。

I wouldn't place the script tag in your render method.我不会在你的渲染方法中放置脚本标签。 Instead, create the variable in your function that you use to do the API call.相反,在您用于执行 API 调用的函数中创建变量。 Here's an example:下面是一个例子:

export default class Form extends Component {

  signup = e => {
    var someVariable = {
      api_key: "api_key_here",
      debug: true
    };
    // other code logic
  }

  render() => {
    <button onClick={this.signup}>Click me!</button>
  }
}

You can use componentWillMount from your app class.您可以从您的应用程序类中使用componentWillMount And add global variables trough it.并通过它添加全局变量。 Like this:像这样:

componentWillMount: function () {
    window.someVariable = {
        api_key: "api_key_here",
        debug: true
    };
}

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

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