[英]React how to change the div name on state change
the section "hdg-alert-popup" , if the state is active it must be "hdg-alert-popup active" while if the "hdg-button-login-container" button is clicked it must be "hdg-alert-popup", how can I do? “hdg-alert-popup”部分,如果 state 处于活动状态,则必须为“hdg-alert-popup active” ,而如果单击“hdg-button-login-container”按钮,则必须为“hdg-alert-popup” “, 我能怎么做?
import parse from 'html-react-parser'; import React, { useEffect, useState } from 'react'; const MessageError = ( titolo: string, descrizione1: string, descrizione2: string, testoBottone: string, ) => { const [active, setActive] = useState(true); if (testoBottone == "") { testoBottone = "Chiudi questo avviso"; } return ( <section className="hdg-alert-popup" > <div className="hdg-login-popup"> <div className="hdg-login-popup-container"> <div className="hdg-login-popup-content"> <h3 className="hdg-title">{parse(titolo)}</h3> <p> {descrizione1} {descrizione2;= "" && <b>{descrizione2}</b>} </p> </div> <div className="hdg-login-popup-error"> <div className="hdg-button-login-container"><a href="#" className="hdg-button-back">{testoBottone}</a></div> </div> </div> </div> </section> ); }; export default MessageError;
Could this be achieved with an active toggle?这可以通过主动切换来实现吗?
import parse from 'html-react-parser';
import React, { useEffect, useState } from 'react';
const MessageError = (
titolo: string,
descrizione1: string,
descrizione2: string,
testoBottone: string,
) => {
const [active, setActive] = useState(true);
if (testoBottone == "") {
testoBottone = "Chiudi questo avviso";
}
return (
<section
className=`hdg-alert-popup ${active ? "active": '' }`
>
<div className="hdg-login-popup">
<div className="hdg-login-popup-container">
<div className="hdg-login-popup-content">
<h3 className="hdg-title">{parse(titolo)}</h3>
<p>
{descrizione1}
{descrizione2 != "" && <b>{descrizione2}</b>}
</p>
</div>
<div className="hdg-login-popup-error">
<div className="hdg-button-login-container" onClick={() => this.setActive(false)}><a href="#" className="hdg-button-back">{testoBottone}</a></div>
</div>
</div>
</div>
</section>
);
};
export default MessageError;
Well, for the button
you can just set the className according with the state:好吧,对于
button
,您可以根据 state 设置className :
<div className={active ? "hdg-alert-popup": "hdg-button-login-container"} onClick={() => setActive(state => !state)}><a href="#" className="hdg-button-back">{testoBottone}</a></div>
For the section
, you can do something similar:对于
section
,您可以执行类似的操作:
<section className={`hdg-alert-popup ${active ? "active" : null}`}>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.