0

我需要将以下脚本转换为 useEffect 函数才能在 react.js 中使用它。

<!DOCTYPE html>
<html>
<body>
<script>
    function startKlarnaOpenBankingXS2AApp(){
        try {
            // Start the flow with the client_token from the flow response.
            window.XS2A.startFlow('eyJhbGciOiJSUzI1NiJ9.eyJzZXNzaW9uX2lkX3Nob3J0IjoiVlZNMjJCOUoiLCJmbG93X3R5cGUiOiJ0cmFuc2FjdGlvbnMiLCJhdXRoX3VybCI6Imh0dHBzOi8vYXV0aGFwaS5wbGF5Z3JvdW5kLm9wZW5iYW5raW5nLmtsYXJuYS5jb20veHMyYS92MS93aXphcmQvdWUxcDY4Z2pzczV0NjBtZmE1ZTNzMWcwY3Y5b3JqMGQiLCJ4czJhX2FwcF91cmwiOiJodHRwczovL3gua2xhcm5hY2RuLm5ldC94czJhL2FwcC92MC9pbmRleC5odG1sI3VlMXA2OGdqc3M1dDYwbWZhNWUzczFnMGN2OW9yajBkIiwid2lkZ2V0X2FwcF91cmwiOiJodHRwczovL3gua2xhcm5hY2RuLm5ldC94czJhL3dpZGdldC1hcHAve3ZlcnNpb259L2luZGV4Lmh0bWwjdWUxcDY4Z2pzczV0NjBtZmE1ZTNzMWcwY3Y5b3JqMGQiLCJ0cmFuc2xhdGlvbnNfdXJsIjoiaHR0cHM6Ly9hdXRoYXBpLnBsYXlncm91bmQub3BlbmJhbmtpbmcua2xhcm5hLmNvbS94czJhL3YxL3RyYW5zbGF0aW9ucy91ZTFwNjhnanNzNXQ2MG1mYTVlM3MxZzBjdjlvcmowZCJ9.JCT199AU9Y0tT4W-6zmx7Fy79gONbn1PKuLayDQep6-DTaf1HBGbO6qWU56qICzIBptM5UjoWkVUponIjzXZLEGCix_82PqUveNJ-w3FuOENjRJOsJ34cT9QSA7r_lCMoGLWWw_dxkv7pvbL7WIbsC3ycxP3ymO78Maa9xnIZSZf7rIRyKXR6TU9jcxZANVyeEoEbl_ZXjyg6GNgOg7HSIYh06XhINzQ4jt38rj5FBW5juXPbGuu_zmHDssZzygwVD3GeIj6zqNhsIL2kkSt_l8aFOAFKzRIo5XpShcQE-Z6Po33JyvOdLKERVIp7aVuxrheR2UZnR1p16G2rTz_lA',
                {
                    onFinished: () => {
                        // Read the flow from the server to retrieve the account list.
                        console.log('onFinished: read the flow from the server to retrieve the account list.')
                    },
                    onError: error => {
                        console.error('onError: something bad happened during the flow.', error)
                    },
                }
            )
        } catch (e) {
            // Handle error that happened while opening the App
            console.error(e)
        }
    }
    window.onXS2AReady = startKlarnaOpenBankingXS2AApp
</script>
<script src="https://x.klarnacdn.net/xs2a/app-launcher/v0/xs2a-app-launcher.js"></script>

</body>
</html>

使用效果函数应如下所示:

  useEffect(() => {
    const script = document.createElement('script');
  
    script.src = "https://x.klarnacdn.net/xs2a/app-launcher/v0/xs2a-app-launcher.js";
    script.async = true;
  
    document.body.appendChild(script);

    return () => {
      document.body.removeChild(script);
    }
  }, []);

如何在 useEffect 中加载 startKlarnaOpenBankingXS2AApp 函数?

谢谢

4

0 回答 0