简体   繁体   English

SetState不更新反应js功能组件中的值

[英]SetState not updating the value in react js functional component

in a functional component i need to show the message to user that email is being sent在功能组件中,我需要向用户显示正在发送 email 的消息
i am changing the html element text by set state我正在通过设置 state 更改 html 元素文本

here is the code这是代码

const [visibility, setVisibility] = useState("hidden");
const [MessageText, setMessageText] = useState("");



 function sendEmail()
    {
    setVisibility('visible');
    setMessageText("Sending email to the recipients.Please wait...");
     functionToSendEmail();
    }


    <label className="cs-label" id="lblMessage" 
style={{ color: '#800000', fontWeight: 'bold', visibility: visibility }}> {MessageText} </label>

here message is not showing in the label element but if instead of setMessageText if i use这里的消息没有显示在label元素中,但是如果我使用而不是setMessageText

document.getElementById('lblMessage').innerHTML = "Email sent to the recipients successfully";

then its working fine showing message to user然后它的工作正常向用户显示消息

On this code you posted, you're not calling sendEmail() function anywhere.在您发布的这段代码中,您没有在任何地方调用 sendEmail() function 。 You may call it, then it will call setMessageText().你可以调用它,然后它会调用 setMessageText()。

Try something like this:尝试这样的事情:

<button onClick={() => sendEmail()}>Send email</button>

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

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