Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
Use object included from global <script> tag inside React component

I am trying to add paypal button using React.

According to paypals dev guide i need to include

<script src="https://www.paypal.com/sdk/js?client-id=YOUR_CLIENT_ID"></script>

in <head></head> in index.html tag and then use

import React from "react";
import ReactDOM from "react-dom"
const PayPalButton = paypal.Buttons.driver("react", { React, ReactDOM });
function YourComponent() {
  const createOrder = (data, actions) => {
    return actions.order.create({
      purchase_units: [
        {
          amount: {
            value: "0.01",
          },
        },
      ],
    });
  };
  const onApprove = (data, actions) => {
    return actions.order.capture();
  };
  return (
    <PayPalButton
      createOrder={(data, actions) => createOrder(data, actions)}
      onApprove={(data, actions) => onApprove(data, actions)}
    />
  );
}

However const PayPalButton = paypal.Buttons.driver("react", {React, ReactDOM});

line throws error

'paypal' is not defined no-undef

It the object included from external <script> isn't loaded. How can i load load object from external <script> into React component then?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There are two ways to square that circle:

const PayPalButton = window.paypal.Buttons.driver("react", { React, ReactDOM });

Remember that in Javascript global variables are also properties of the global object (window in the browser). I actually like doing this for globals, because it makes it very clear in the code that this thing is a global variable rather than reading it and going "WTF where did that come from?".

You can also (assuming ESLint) suppress the lint warning directly (NOT recommended):

// eslint-disable-line no-undef
const PayPalButton = paypal.Buttons.driver("react", { React, ReactDOM });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda