简体   繁体   English

如何使用异步值(Reactjs)正确加载全局变量?

[英]How to properly load global variable with async values(Reactjs)?

I'm trying to solve this problem that I can't seem to solve with stripe's API's我正在尝试解决这个我似乎无法用 Stripe 的 API 解决的问题

So when creating a charge with their new version API they say that in the front end we should call因此,当使用他们的新版本 API 创建费用时,他们说在前端我们应该调用

loadStripe('publishable Key',{'Connected account ID'})

and set that to a const.并将其设置为常量。

now I dont undestand how are we supposed to get the ID that is stored somewhere say a database?现在我不明白我们应该如何获取存储在某个数据库中的 ID?

As a reference please look at this and here (In Step 3 ...).作为参考,请查看这里这里(在第 3 步中...)。

What I'm currently doing is something like this我目前正在做的是这样的

import React from "react";
import ReactDOM from "react-dom";
import { Elements } from "@stripe/react-stripe-js";
import { loadStripe } from "@stripe/stripe-js";
import CheckoutForm from "./CheckoutForm";

//btw I have set this to const and to let and none work
const stripePromise = fetch("url", {
  method: "POST",
  headers: {
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    anything: window.sessionStorage.getItem("Variable Account")
    //here store something that  will tell what account to pull ID data from
  })
})
  .then(data => data.json())
  .then(result => {
    return loadStripe("KEY", { stripeAccount: result });
  });

class App extends React.Component {
  render() {
    return (
      <Elements stripe={stripePromise}>
        <CheckoutForm />
      </Elements>
    );
  }
}

export default App;

but the const seems to not load correctly if one goes with the regular flow of the app say from但是如果按照应用程序的常规流程,const 似乎无法正确加载

myapp.com/home  

-> click -> 点击

myapp.com/home/something  

-> then -> 然后

myapp.com/home/something/payment 

stripe is not loading but one refreshes the browser now works but that tells me I'm doing maybe something wrong or I have to make the app refresh in 'componentDidMount()' maybe? stripe 未加载但刷新浏览器现在可以工作,但这告诉我我可能做错了什么,或者我必须在“componentDidMount()”中刷新应用程序?

One can set it to be static but connected accounts can be many so if anyone can help me with this I would appreciate it可以将其设置为静态,但连接的帐户可能很多,因此如果有人可以帮助我,我将不胜感激

Generally, you'd want to have this account ID available in your app.通常,您希望在您的应用程序中使用此帐户 ID。 But if you need to retrieve it, that's fine, but make sure the stripePromise is what you think it is.但是如果您需要检索它,那很好,但要确保stripePromise是您认为的那样。 For example, I can make this work here with a simulated fetch call here: https://codesandbox.io/s/stripe-connect-w-resolve-wts34例如,我可以在这里通过模拟 fetch 调用来完成这项工作: https : //codesandbox.io/s/stripe-connect-w-resolve-wts34

Note that I'm managing the Promise explicitly:请注意,我正在明确管理 Promise:

const stripePromise = new Promise((resolve, reject) => {
  fetch(...)
  .then(data => data.json())
  .then(result => {
    resolve(
      loadStripe(STRIPE_PUBKEY, { stripeAccount: "acct_xxx" })
    );
  });
});

The fact that you describe this breaking with navigation suggests you might be routing incorrectly.您用导航描述这种中断的事实表明您可能路由不正确。 If this is a single page app, the navigation shouldn't cause the App component to re-render.如果这是单页应用程序,导航不应导致App组件重新呈现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM