简体   繁体   English

谁能帮我在这个 React JS 组件中隐藏 setTimeout id?

[英]Can anyone help me to hide the setTimeout id in this React JS component?

I'm trying to do what I thought would be a simple task.我正在尝试做我认为是一项简单的任务。 On submit of the form that is pulled in by the GravityForm component, I set the handleSubmit state to true which then renders the thank you message (this all works fine, please forgive the fact that I've removed the urls but I can assure you this bit is fine).在提交由 GravityForm 组件拉入的表单时,我将 handleSubmit 状态设置为 true,然后呈现感谢消息(这一切正常,请原谅我已经删除了网址,但我可以向您保证这一点很好)。

My issue comes when I load the success message.当我加载成功消息时,我的问题就出现了。 The setTimeout function displays the id. setTimeout 函数显示 id。 Is there a way I can either stop it displaying that id or implement this function in a different way that means it won't show?有没有一种方法可以阻止它显示该 id 或以不同的方式实现此功能,这意味着它不会显示?

The expected functionality is that the thank you message will display for 3 seconds and then the page will load to a different site.预期的功能是感谢消息将显示 3 秒钟,然后页面将加载到不同的站点。

Thank you谢谢

import "./form.css";
import React, { Component } from "react";
import GravityForm from "react-gravity-form";
import styled from "styled-components";

export class  Gravity extends Component {

    state = {
        handleSubmit : false,
    }

    successMessage = styled.div`
        display: block;
        color: #fff;
        font-size: 24px;
        text-align: center;
    `;

    render() {
        const { handleSubmit } = this.state;

        if (!handleSubmit) {
            return (
                <GravityForm
                    backendUrl="https://removedurlforclientprivacy.com/wp-json/glamrock/v1/gf/forms"
                    formID="3"
                    onSubmitSuccess={ () => {
                        this.setState({
                            handleSubmit : true,
                        })
                    } }
                    
                />
            );
        } else {
            return (
                <>
                    <this.successMessage>
                        <p>Thanks for entering our competition!<br />If you're our lucky winner, we will let you know.</p>
                    </this.successMessage>
                    { setTimeout(() => window.location.href = 'https://google.co.uk', 3000) }
                </>
            )
            
        }

    }
}

export default Gravity

Can you please try this way, Create a function which you can set your success things and call it on your else condition你能试试这种方式吗,创建一个函数,你可以设置成功的东西并在你的其他条件下调用它

renderSuccesssMessage = () => {
  setTimeout(() => window.location.href = 'https://google.co.uk', 3000)
  return (
    <this.successMessage>
    <p>Thanks for entering our competition!<br />If you're our lucky winner, we will let you know.</p>
</this.successMessage>
  )
}

And Just call this function into your else condtion只需将此函数调用到您的其他条件中

 else {
        return (
            this.renderSuccessMessage()
        )
        
    }

The reason you're seeing the id is because the setTimeout function returns the id.您看到 id 的原因是 setTimeout 函数返回了 id。 Imagine the setTimeout() call simply being replaced with 123, so that it would look like { 123 }, it will of course show the 123 value.想象一下 setTimeout() 调用只是被 123 替换,因此它看起来像 { 123 },它当然会显示 123 值。

A way you can suppress the value is by converting it into an expression to be evaluated - something like { 123 && <></> }, that way the empty element will be returned instead of the value itself (obviously replacing the 123 with your setTimeout() function as follows:您可以抑制该值的一种方法是将其转换为要评估的表达式 - 类似于 { 123 && <></> },这样将返回空元素而不是值本身(显然,将 123 替换为您的setTimeout() 函数如下:

{ setTimeout(() => window.location.href = 'https://google.co.uk', 3000) && <></> }

You could also play around with { 123 && undefined } or { 123 && null }, which would likely result in not even returning an element at all, again ensuring to replace 123 with your setTimeout() function.您还可以使用 { 123 && undefined } 或 { 123 && null },这可能会导致根本不返回元素,再次确保用 setTimeout() 函数替换 123。

You can change your onSubmitSuccess function like below one and remove setTimeout from else block :您可以更改您的 onSubmitSuccess 函数,如下所示,并从 else 块中删除 setTimeout :

onSubmitSuccess={() => {
                    this.setState({
                        handleSubmit : true,
                    },() => {
                      setTimeout(() => window.location.href = 'https://google.co.uk', 3000)
                    });
                }}

You can write it to the console or use an empty auxiliary function.您可以将其写入控制台或使用空的辅助功能。


function do_nothing () {}


{ console.info(setTimeout(() => window.location.href = 'https://google.co.uk', 3000)) }
{ do_nothing(setTimeout(() => window.location.href = 'https://google.co.uk', 3000)) }

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

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