score:4

Accepted answer
const addpaypalscript = () => {
    const script = document.createelement("script");
    script.type = "text/javascript";
    script.src = `https://www.paypal.com/sdk/js?client-id=${process.env.paypal_client_id}`;
    // script.setattribute("data-namespace", "paypal_sdk");

    script.async = true;
    script.onload = () => {
      setsdkready(true);
    };
    document.body.appendchild(script);
  };

script.onload will flag that it is ready. track it in usestate

   const [sdkready, setsdkready] = usestate(false);

then call addpaypalscript inside useeffect or componentdidmount. i use useeffect

useeffect(() => {
dispatch(orderdetailrequeststart(paramsid));

  if (!window.paypal) {
    addpaypalscript();
  } else {
    // flags that it is ready
    setsdkready(true);
  }

}, []);

Related Query

More Query from same tag