简体   繁体   中英

Reload a page after the initial load with Next.js

I have a route called "./checkout" that renders embedded elements from Xola. The issue is I am using client side routing and the page needs a refresh to load the checkout page correctly (if not, Xola elements do not show up on the DOM 1 ). When I try to reload the page on the initial load I get an infinite reload loop. I can't use a href for specific reasons so I need to continue to use Next.js routing. Anyway I can go about this?1

After refresh页面刷新后

checkout.js

import Head from "next/head";
import { useRouter } from "next/router";
import { Container, Button } from "@mui/material";
import { makeStyles } from "@mui/styles";
import { CheckoutCard } from "../components/layout/directory";
import useIsSsr from "@/config/useSsr";

function Checkout() {
  const isSsr = useIsSsr();
  const url = isSsr ? null : window.location.search;
  const router = useRouter();
  const classes = useStyles();

  return (
    <>
      {isSsr ? null : window.location.reload()}
      <Head>
        <title>checkout</title>
      </Head>
      <Container className={classes.root}>
        <Button
          className={classes.btn}
          onClick={router.back}
          color="secondary"
          variant={"contained"}
        >
          back
        </Button>
        <CheckoutCard
          url={`https://checkout.xola.com/index.html#seller/5f3d889683cfdc77b119e592/experiences/${
            url && url.slice(1)
          }?openExternal=true`}
        />
      </Container>
    </>
  );
}

const useStyles = makeStyles((theme) => ({
  root: { marginTop: theme.spacing(10) },
  btn: { marginBottom: theme.spacing(5) },
}));

export default Checkout;

CheckoutCard.js

function CheckoutCard({ url }) {
  return (
    <div
      className="xola-embedded-checkout"
      data-seller="5f3d889683cfdc77b119e592"
      data-experience="5f3d8d80d6ba9c6b14748160"
      data-version="2"
      id="xola-checkout"
    ></div>
  );
}

export default CheckoutCard;

Please add one more prop to CheckoutCard component calling in checkout.js.
You need to update

<CheckoutCard
          url={`https://checkout.xola.com/index.html#seller/5f3d889683cfdc77b119e592/experiences/${
            url && url.slice(1)
          }?openExternal=true`}
        />

to

<CheckoutCard
          url={`https://checkout.xola.com/index.html#seller/5f3d889683cfdc77b119e592/experiences/${
            url && url.slice(1)
          }?openExternal=true`}
          key={new Date().getTime()}
        />

"key" prop is to identify the component and you are going to use external service ( like iframe, not sure correctly ) So in order to render the embedded elements from Xola, you should add "key" prop for CheckoutCard component calling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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