我在将 Stripe 与 React.js 集成时遇到问题。我面临的错误消息是:“我们无法从指定的元素检索数据。请确保您尝试使用的元素仍然已安装。”
以下是相关代码片段:
`// WithStripe Component
import React from "react";
import { useSelector } from "react-redux";
import { Elements } from "@stripe/react-stripe-js";
import { loadStripe } from "@stripe/stripe-js";
function WithStripe({ children }) {
const { stripeKey } = useSelector((state) => state?.stripeReducer);
let promise = null;
if (stripeKey) {
promise = loadStripe(stripeKey);
}
const { language } = useSelector((state) => state?.AppReducer);
return (
<Elements
stripe={promise}
options={{
locale: language,
}}
>
{children}
</Elements>
);
}
export default WithStripe;
// Entry point
import React, { Suspense } from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./app/Index";
import WithStripe from "components/Hoc/WithStripe";
ReactDOM.render(
<WithStripe>
<App />
</WithStripe>,
document.getElementById("root")
);`