在 React 中访问变量 index.html 对于 Branch SDK

Access Variable In React index.html For Branch SDK

如果我将此 SDK 添加到我的 index.html 文件中,我将如何无法在我的其他组件中访问它。还有其他方法吗?

  <script>
    // load Branch
      (function(b,r,a,n,c,h,_,s,d,k){if(!b[n]||!b[n]._q){for(;s<_.length;)c(h,_[s++]);d=r.createElement(a);d.async=1;d.src="https://cdn.branch.io/branch-latest.min.js";k=r.getElementsByTagName(a)[0];k.parentNode.insertBefore(d,k);b[n]=h}})(window,document,"script","branch",function(b,r){b[r]=function(){b._q.push([r,arguments])}},{_q:[],_v:1},"addListener applyCode autoAppIndex banner closeBanner closeJourney creditHistory credits data deepview deepviewCta first getCode init link logout redeem referrals removeListener sendSMS setBranchViewData setIdentity track validateCode trackCommerceEvent logEvent disableTracking".split(" "), 0);
    // init Branch
    branch.init('key_live_kaFuWw8WvY7yn1d9yYiP8gokwqjV0Swt');
  </script>

试图在另一个组件中以这种方式访问​​它。

var options = { no_journeys: true };
branch.init('key_live_kaFuWw8WvY7yn1d9yYiP8gokwqjV0Swt', options, function(err, data) {
  console.log(err, data); 
});

您可以将 branch 绑定到 window 属性 并从整个应用程序访问它们。

初始化:

<head>
  <script>
    branch.init('key_live_kaFuWw8WvY7yn1d9yYiP8gokwqjV0Swt', options, function(err, data) {
      window.__BRANCH__ = data;
    });
  </script>
</head>

用法:

const Component = () => (
  <button 
    onClick={() => {
      // You have access to window.__BRANCH__
    }}
  >
    Button
  </button>
);

使用npm

https://github.com/BranchMetrics/web-branch-deep-linking#quick-install

npm i branch-sdk

然后:

import branch from 'branch-sdk'

branch.init(...)