简体   繁体   English

在 React 中,模态内部的组件在模态打开之前准备好/渲染

[英]In React, component inside of a modal to be ready/rendered before the modal is open

I have a react component that opens a modal window, and inside of that modal window, there is a second component, a form.我有一个打开模式窗口的反应组件,在该模式窗口内,有第二个组件,一个表单。

The form needs to communicate with an external service, as it loads an iframe.表单在加载 iframe 时需要与外部服务通信。

When the page loads, the form component does not exist, so when I open the modal, the form component is rendered, THEN it communicates with the external service, and returns the iframe, so it takes a few seconds to load.页面加载时,表单组件不存在,所以当我打开modal时,表单组件被渲染,然后它与外部服务通信,并返回iframe,所以加载需要几秒钟。

Is there any way to have this component ready when the page first loads?有没有办法在页面首次加载时准备好这个组件? Before the modal is actually opened?在模态实际打开之前?

The code:编码:

const HubspotForm = ({ id, redirect = false }) => {
    const script = document.createElement('script');
    script.src = 'https://js.hsforms.net/forms/v2.js';
    document.body.appendChild(script);
    const jqScript = document.createElement('script');
    jqScript.src =
        'https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js';
    document.body.appendChild(jqScript);
    const base_url = window.location.origin;

    script.addEventListener('load', () => {
        if (window.hbspt) {
            window.hbspt.forms.create({
                portalId: 'xxx',
                formId: 'xxx',
                target: '#hubspotForm',
                inlineMessage:
                    "<p style='text-align:center;padding:100px 30px'>Redirecting, please wait...</p>",

                onFormSubmit: function ($form) {
                    let redirect_url = `${base_url}/thank-you/?goal=1`;
                    setTimeout(function () {
                        window.location = redirect_url;
                    }, 250);
                },
            });
        }
    });

    return (
        <div>
            <div
                id="hubspotForm"
                css={css`
                    input[type='submit'] {
                        background-color: var(--primary) !important;
                    }
                `}
            ></div>
        </div>
    );
};

return (
    <StyledDialog
        onClose={handleClose}
        aria-labelledby="simple-dialog-title"
        open={open}
    >
        <div className="modal-close" onClick={handleClose}>
            <GrClose color="red" />
        </div>
        <h3>{demoRequest.demoRequestFormTitle}</h3>

        <p>{demoRequest.demoRequestFormText}</p>
        <HubspotForm redirect={true} />
    </StyledDialog>
);

... ...

export default function SimpleDialogDemo({
    btnText = 'Talk To An Expert',
    variant = 'contained',
    white = false,
}) {

    const classes = useStyles();
    const [open, setOpen] = React.useState(false);

    const handleClickOpen = () => {
        setOpen(true);
    };

    const handleClose = (value) => {
        setOpen(false);
    };

    return (
        <>
            <Button
                variant="contained"
                color="primary"
                className={white ? classes.btnWhite : classes.btn}
                disableElevation={white ? true : false}
                onClick={handleClickOpen}
            >
                {btnText}
            </Button>
            <SimpleDialog open={open} onClose={handleClose} />
        </>
    );
}

您可以尝试在 css 中使用 display:none 和 zIndex: -1... 并在默认情况下将您的 openModal 属性设置为 true

It seems that you have two jobs.看来你有两份工作。

  1. Display the modal显示模态
  2. Acquire the needed information to open the iframe.获取打开 iframe 所需的信息。

I would get the info initially somewhere else in the load function and store it in the state.我最初会在加载函数的其他地方获取信息并将其存储在状态中。 So when the modal opens, the state will be already set and no overhead in loading time is added.因此,当模式打开时,状态将已经设置,并且不会增加加载时间的开销。

Is there any specific reason the communication is initiating when the modal opens?模式打开时是否有任何特定原因发起通信?

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

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