[英]How to use third party extension in react app?
I am implementing razorpay payment in my react app. 我正在我的应用程序中实现razorpay付款。 Below is the code given in the doc.
以下是文档中给出的代码。
<button id="rzp-button1">Pay</button>
<script src = "https://checkout.razorpay.com/v1/checkout.js" > < / script>
<script>
var options = {
"key": "YOUR_KEY_ID",
"amount": "2000", // 2000 paise = INR 20
"name": "Merchant Name",
"description": "Purchase Description",
"image": "/your_logo.png",
"handler": function (response) {
alert(response.razorpay_payment_id);
},
"prefill": {
"name": "Harshil Mathur",
"email": "harshil@razorpay.com"
},
"notes": {
"address": "Hello World"
},
"theme": {
"color": "#F37254"
}
};
var rzp1 = new Razorpay(options);
document.getElementById('rzp-button1').onclick = function (e) {
rzp1.open();
e.preventDefault();
}
</script>
So how do I implement it in react. 那么我如何在反应中实现它。 I can put onClick on the button and can call the rzp1.open();.
我可以在按钮上单击onClick,然后可以调用rzp1.open();。 But I think it will through undefined for that.
但是我认为这将是不确定的。 Also the https://checkout.razorpay.com/v1/checkout.js should it be loaded from index.html file?
还应该从index.html文件加载https://checkout.razorpay.com/v1/checkout.js吗? I am very much confused here.
我在这里很困惑。 Please help me out.
请帮帮我。
You can use third party libraries by applying them in componentDidMount()
. 您可以通过在
componentDidMount()
应用第三方库来使用它们。 ie you can bind library
to DOM
after it's rendered. 即,您可以在呈现
library
之后将library
绑定到DOM
。
In your case library
don't need DOM
element but certain options not related to DOM
. 在您的情况下,
library
不需要DOM
元素,但是某些与DOM
不相关的选项。 So you can also initialise it before rendering your component
. 因此,您也可以在呈现
component
之前对其进行初始化。
An example to your case. 您的案例的一个例子。
class YourComponentWithButton extends React.Component{
constructor(props){
super(props);
this.state = {
rzp1: null //holds your external library instance
//your initial state if any
}
}
componentDidMount(){ //you can also keep this code in componentWillMount()
var options = {
"key": "YOUR_KEY_ID",
"amount": "2000", // 2000 paise = INR 20
"name": "Merchant Name",
"description": "Purchase Description",
"image": "/your_logo.png",
"handler": function (response) {
alert(response.razorpay_payment_id);
},
"prefill": {
"name": "Harshil Mathur",
"email": "harshil@razorpay.com"
},
"notes": {
"address": "Hello World"
},
"theme": {
"color": "#F37254"
}
};
this.setState({
rzp1 : new window.Razorpay(options)
})
}
buttonClick(event){
if(state.rzp1){ //sanity check whether library loaded to varibale
this.state.rzp1.open();
}
e.preventDefault();
}
render(){
return(
<div className="your-container">
<button onClick={this.buttonClick.bind(this)}>Your Button</button>
</div>
)
}
}
if you want to setup a react component to do this I would recommend you make it extendable and reusable (thats what react components are for!) 如果您想设置一个反应组件来做到这一点,我建议您使其可扩展和可重用(这就是反应组件的作用!)
class RazorPay extends Component {
constructor(props){
super(props);
this.options = Object.assign(
{},
{
"key": "YOUR_KEY_ID",
"amount": "2000", // 2000 paise = INR 20
"name": "Merchant Name",
"description": "Purchase Description",
"image": "/your_logo.png",
"handler": (response) => {
alert(response.razorpay_payment_id);
},
"prefill": {
"name": "Harshil Mathur",
"email": "harshil@razorpay.com"
},
"notes": {
"address": "Hello World"
},
"theme": {
"color": "#F37254"
}
},
props.rzp1 // any options you pass via props will override the defaults
);
}
componentWillMount(){
this.rzp1 = new window.Razorpay(options);
}
handleClick = (e) =>
this.rzp1.open();
e.preventDefault();
}
render(){
return(
<div>
<button onClick={this.handleClick}>Open</button>
</div>
)
}
}
to use this component and pass a different amount to charge you would just do something like this 要使用此组件并传递不同的金额以进行充电,您只会执行以下操作
const someOptions = {amount: '100'}; // somewhere in the render method.
<RazorPay rzp1={someOptions} /> // somewhere in the return of the render method
recommendations: move most of those default options to a config file that you can import and use. 建议:将大多数默认选项移到可导入和使用的配置文件中。 try to abstract as much as you can.
尽可能地抽象。 it'll make your code easier to use
这将使您的代码更易于使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.