useEffect 需要调用函数,并在 5 秒后进行渲染。

huangapple go评论73阅读模式
英文:

useEffect needs to call function and render it after 5 seconds

问题

以下是您要翻译的代码部分:

import React, { useState, useEffect } from "react";
import { MdDone } from "react-icons/md";
import { getCookie, setCookie } from 'tiny-cookie';

const CookieNotice = () => {
    const [items, setItems] = useState({
        name: "zureaCookie",
        value: ""
    });

    useEffect(() => {
        const delay = 5000;
        const timeoutId = setTimeout(() => {
            renderCookie();
        }, delay);
        return () => {
            clearTimeout(timeoutId);
        };
    }, []);

    const acceptCookie = (e) => {
        e.preventDefault();
        let newItemsObj = { ...items };
        newItemsObj.value = "Accepted";
        setItems(newItemsObj);
        setCookie(items.name, newItemsObj.value, { expires: '1Y' });
    }

    const declineCookie = (e) => {
        e.preventDefault();
        let newItemsObj = { ...items };
        newItemsObj.value = "Declined";
        setItems(newItemsObj);
        setCookie(items.name, newItemsObj.value, { expires: '1Y' });
    }

    const renderCookie = () => {
        const zureaCookie = getCookie(items.name);
        return (
            <>
                {!zureaCookie ? (<div className="cookie-notice-container">
                    <div className="cookie-notice-wrapper">
                        <div className="cookie-notice-content">
                            <p>We use cookies to improve your experience on our website. By browsing this website,
                                you agree to our use of cookies.</p>
                        </div>
                        <div className="cookie-notice-btn-wrapper">
                            <a href="" className="btn-terms">Terms & Conditions</a>
                            <a href="" className="btn-decline" onClick={declineCookie}>Decline</a>
                            <a href="" className="btn-accept" onClick={acceptCookie}>Accept<MdDone className="md-done" /></a>
                        </div>
                    </div>
                </div>) : (null)}
            </>
        );

    }

    return renderCookie();

}

export default CookieNotice;
英文:

I need to show cookies after 5 seconds on the home pages. The function renderCookie works fine but I'm not sure how to implement useUffect and to call function renderCookie after 5 seconds. At the moment it runs immediately but not after 5 seconds. Anyone have any suggestions, I'm not sure where I went wrong?

import React, {useState, useEffect} from &quot;react&quot;;
import { MdDone } from &quot;react-icons/md&quot;;
import {getCookie, setCookie} from &#39;tiny-cookie&#39;;
const CookieNotice = () =&gt; {
const [items, setItems] = useState({
name: &quot;zureaCookie&quot;,
value: &quot;&quot;
});
useEffect(() =&gt; {
const delay = 5000; 
const timeoutId = setTimeout(() =&gt; {
renderCookie();
}, delay);
return () =&gt; {
clearTimeout(timeoutId);
};
}, []);
const acceptCookie = (e) =&gt; {
e.preventDefault();
let newItemsObj = {...items};
newItemsObj.value = &quot;Accepted&quot;;
setItems(newItemsObj);
setCookie(items.name, newItemsObj.value, { expires: &#39;1Y&#39; });
}
const declineCookie = (e) =&gt; {
e.preventDefault();
let newItemsObj = {...items};
newItemsObj.value = &quot;Declined&quot;;
setItems(newItemsObj);
setCookie(items.name, newItemsObj.value, { expires: &#39;1Y&#39; });
}
const renderCookie = () =&gt; {
const zureaCookie = getCookie(items.name);
return (
&lt;&gt;
{!zureaCookie ? (&lt;div className=&quot;cookie-notice-container&quot;&gt;
&lt;div className=&quot;cookie-notice-wrapper&quot;&gt;
&lt;div className=&quot;cookie-notice-content&quot;&gt;
&lt;p&gt;We use cookies to improve your experience on our website. By browsing this website,
you agree to our use of cookies.&lt;/p&gt;
&lt;/div&gt;
&lt;div className=&quot;cookie-notice-btn-wrapper&quot;&gt;
&lt;a href=&quot;&quot; className=&quot;btn-terms&quot;&gt;Terms &amp; Conditions&lt;/a&gt;
&lt;a href=&quot;&quot; className=&quot;btn-decline&quot; onClick={declineCookie}&gt;Decline&lt;/a&gt;
&lt;a href=&quot;&quot; className=&quot;btn-accept&quot; onClick={acceptCookie}&gt;Accept&lt;MdDone className=&quot;md-done&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;) : (null)}
&lt;/&gt;);
}
return renderCookie();
}
export default CookieNotice;

答案1

得分: 3

这是您提供的代码的中文翻译:

import React, { useState, useEffect } from "react";
import { MdDone } from "react-icons/md";
import { getCookie, setCookie } from "tiny-cookie";

const CookieNotice = () => {
  const [items, setItems] = useState({
    name: "zureaCookie",
    value: "",
  });
  const [elapsed, setElapsed] = useState(false);

  useEffect(() => {
    const delay = 5000;
    const timeoutId = setTimeout(() => {
      setElapsed(true);
    }, delay);
    return () => {
      clearTimeout(timeoutId);
    };
  }, []);

  const acceptCookie = (e) => {
    e.preventDefault();
    let newItemsObj = { ...items };
    newItemsObj.value = "Accepted";
    setItems(newItemsObj);
    setCookie(items.name, newItemsObj.value, { expires: "1Y" });
  };

  const declineCookie = (e) => {
    e.preventDefault();
    let newItemsObj = { ...items };
    newItemsObj.value = "Declined";
    setItems(newItemsObj);
    setCookie(items.name, newItemsObj.value, { expires: "1Y" });
  };

  const renderCookie = () => {
    const zureaCookie = getCookie(items.name);
    return (
      <>
        {!zureaCookie && elapsed ? (
          <div className="cookie-notice-container">
            <div className="cookie-notice-wrapper">
              <div className="cookie-notice-content">
                <p>
                  我们使用cookie来提升您在我们网站的体验通过浏览本网站您同意我们使用cookie
                </p>
              </div>
              <div className="cookie-notice-btn-wrapper">
                <a href="" className="btn-terms">
                  条款和条件
                </a>
                <a href="" className="btn-decline" onClick={declineCookie}>
                  拒绝
                </a>
                <a href="" className="btn-accept" onClick={acceptCookie}>
                  接受
                  <MdDone className="md-done" />
                </a>
              </div>
            </div>
          </div>
        ) : null}
      </>
    );
  };

  return renderCookie();
};

export default CookieNotice;

请注意,代码中的HTML标签和文本内容已经翻译成了中文。

英文:
import React, { useState, useEffect } from &quot;react&quot;;
import { MdDone } from &quot;react-icons/md&quot;;
import { getCookie, setCookie } from &quot;tiny-cookie&quot;;
const CookieNotice = () =&gt; {
const [items, setItems] = useState({
name: &quot;zureaCookie&quot;,
value: &quot;&quot;,
});
const [elapsed, setElapsed] = useState(false);
useEffect(() =&gt; {
const delay = 5000;
const timeoutId = setTimeout(() =&gt; {
setElapsed(true);
}, delay);
return () =&gt; {
clearTimeout(timeoutId);
};
}, []);
const acceptCookie = (e) =&gt; {
e.preventDefault();
let newItemsObj = { ...items };
newItemsObj.value = &quot;Accepted&quot;;
setItems(newItemsObj);
setCookie(items.name, newItemsObj.value, { expires: &quot;1Y&quot; });
};
const declineCookie = (e) =&gt; {
e.preventDefault();
let newItemsObj = { ...items };
newItemsObj.value = &quot;Declined&quot;;
setItems(newItemsObj);
setCookie(items.name, newItemsObj.value, { expires: &quot;1Y&quot; });
};
const renderCookie = () =&gt; {
const zureaCookie = getCookie(items.name);
return (
&lt;&gt;
{!zureaCookie &amp;&amp; elapsed ? (
&lt;div className=&quot;cookie-notice-container&quot;&gt;
&lt;div className=&quot;cookie-notice-wrapper&quot;&gt;
&lt;div className=&quot;cookie-notice-content&quot;&gt;
&lt;p&gt;
We use cookies to improve your experience on our website. By browsing this website, you agree to our
use of cookies.
&lt;/p&gt;
&lt;/div&gt;
&lt;div className=&quot;cookie-notice-btn-wrapper&quot;&gt;
&lt;a href=&quot;&quot; className=&quot;btn-terms&quot;&gt;
Terms &amp; Conditions
&lt;/a&gt;
&lt;a href=&quot;&quot; className=&quot;btn-decline&quot; onClick={declineCookie}&gt;
Decline
&lt;/a&gt;
&lt;a href=&quot;&quot; className=&quot;btn-accept&quot; onClick={acceptCookie}&gt;
Accept
&lt;MdDone className=&quot;md-done&quot; /&gt;
&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
) : null}
&lt;/&gt;
);
};
return renderCookie();
};
export default CookieNotice;

huangapple
  • 本文由 发表于 2023年2月24日 03:43:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定