我试图将Quickbooks Button(https://developer.intuit.com/docs/0100_quickbooks_online/0100_essentials/000500_authentication_and_authorization/widgets#/Connect_to_QuickBooks_button)连接到React组件中,并且试图复制以下方法:Adding script tag to React/JSX

Quickbooks按钮使用以下脚本代码:

<script
     type="text/javascript"
     src="https://appcenter.intuit.com/Content/IA/intuit.ipp.anywhere-1.3.3.js">
</script>
<script src="https://js.appcenter.intuit.com/Content/IA/intuit.ipp.anywhere-1.3.3.js" type="text/javascript"></script>
<script type="text/javascript">
    intuit.ipp.anywhere.setup({
            grantUrl: 'http://www.mycompany.com/HelloWorld/RequestTokenServlet',
            datasources: {
                 quickbooks : true,
                 payments : true
           },
            paymentOptions:{
                  intuitReferred : true
           }
    });
</script>
<body>
    <ipp:connectToIntuit></ipp:connectToIntuit>
</body>

我试图使用下面的React代码,但无法正常工作。任何帮助表示赞赏。谢谢。
import React from 'react';

class ConnectToQuickBooksOnlineButton extends React.Component {
    constructor(props){
        super(props);
        this.state = {
        };
    }

    componentWillMount() {
            const library = document.createElement("script");
            library.src = "https://appcenter.intuit.com/Content/IA/intuit.ipp.anywhere-1.3.3.js";
            library.type = "text/javascript"
            library.async = true;
            document.body.appendChild(library);

            const setup = document.createElement("script");
            setup.src = "https://js.appcenter.intuit.com/Content/IA/intuit.ipp.anywhere-1.3.3.js";
            setup.type = "text/javascript";
            setup.async = true;
            document.body.appendChild(setup);

            const connect = document.createElement("script");
            connect.type = "text/javascript";
            connect.innerHTML = "intuit.ipp.anywhere.setup({grantUrl: '/quickbooksauth',datasources: {quickbooks : true, payments : true}, paymentOptions:{intuitReferred : true}});"
            document.body.appendChild(connect);

            const body = document.createElement("body");
            body.innerHTML = "<ipp:connectToIntuit></ipp:connectToIntuit>";
            body.async = true;
            document.body.appendChild(body);
    }

    render(){
        return <div />;
    }
};

export default ConnectToQuickBooksOnlineButton;

我尝试将脚本内容放入index.html中,并从Quickbooks组件中调用它。该按钮仍然没有显示。
import React from 'react';

class ConnectToQuickBooksOnlineButton extends React.Component {
    constructor(props){
        super(props);
        this.state = {
        };
    }

    componentWillMount() {
        const connectToIntuit = document.createElement('ipp:connectToIntuit');
        document.body.appendChild(connectToIntuit);
    }

    render(){
        return <div ref="ipp:connectToIntuit"/>;
    }
};

export default ConnectToQuickBooksOnlineButton;

我还创建了以下 fiddle :https://jsfiddle.net/aewhatley/7eL716mp/

最佳答案

@alex似乎您正在尝试在渲染之前访问Dom,您应该使用“componentDidMount”将intuit按钮加载到div或容器上。

我已经创建了一个WebpackBin来说明实现,请告诉我这是为什么要实现?

这是我创建的简单组件

QuickBooks.js

工作演示WebpackBin https://www.webpackbin.com/bins/-Kg6yu5JUmy9dD08JI_A

import React from 'react'

class QuickBooks extends React.Component {
  constructor(props){
    super(props);

    window.intuit.ipp.anywhere.setup({
      grantUrl: 'http://www.mycompany.com/HelloWorld/RequestTokenServlet',
      datasources: {
        quickbooks : true,
        payments : true
      },
      paymentOptions:{
        intuitReferred : true
      }
    })

  }
  componentDidMount() {
      let buttonContainer = document.getElementById("intuButton")
      const connectToIntuit = document.createElement('ipp:connectToIntuit');
      buttonContainer.appendChild(connectToIntuit);
  }
  render(){
      return (
          <div id="intuButton"></div>
      )
  }
}

export default QuickBooks
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.0/react-dom.min.js"></script>

关于javascript - 如何将React组件连接到Quickbooks按钮,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42942413/

10-09 22:25