我正在尝试将Quickbooks按钮(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;
我还创建了以下小提琴:https://jsfiddle.net/aewhatley/7eL716mp/
答案 0 :(得分:6)
@alex看起来你在渲染之前试图访问Dom,你应该使用&#34; componentDidMount&#34;在div或容器上加载你的intuit按钮。
我已经创建了一个说明实现的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
&#13;
<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>
&#13;
答案 1 :(得分:0)
您在哪里渲染ConnectToQuickBooksOnlineButton组件?比如index.jsx或app.jsx?您可以在index.html中包含所有脚本,或者在根组件(index / app.jsx)中创建它们,在ConnectToQuickBooksOnlineButton中只创建所需的html标记,如下所示:
componentWillMount() {
const connectToIntuit = document.createElement('ipp:connectToIntuit')
document.body.appendChild(connectToIntuit)
}
答案 2 :(得分:0)
你不需要。 只需使用您的后端生成链接即可。
// backend (expressJs)
router.post('/authUri', async (req: Request, res: Response, next: NextFunction) => {
const authUri = oauthClientGlobal.authorizeUri({
scope: [OAuthClient.scopes.Accounting],
state: 'intuit-test',
})
res.status(200).json(authUri)
})
然后前端(反应) 调用您的后端路由以获取 Quickbooks URL 然后通过响应将页面重定向到快速手册:
window.location.href = authUri // response Quickbooks URL From Backend