[英]show bootstrap modal after cookie check in reactjs (not use react-bootstrap or something similar only use cdn bootstrap)
我想在網站加載后顯示模態並檢查cookie,如果cookie未設置顯示模態,如果cookie設置不顯示模態。 我使用引導模式而不是 react-bootstrap 或類似的東西,這就是我在 vanila javascript 和 html 中顯示模式的方式:
function checkCookie() {
let tampil = (getCookie("janganTampil") === 'true');
if (tampil != true) {
checkbox.checked = false;
myModal.show();
} else {
checkbox.checked = true;
}
}
<body onload="checkCookie()">
...
<div class="modal fade" id="modal" tabindex="-1" aria-labelledby="modalLabel" aria-hidden="true">
...
</div>
</body>
show()
是來自引導程序的 function。 我嘗試在 react 上運行show()
但出錯,因為 function 未知。 如何在反應中顯示模態? 反應代碼:
應用程序
const [isCookieSet, setIsCookieSet] = useState(false);
function checkCookie() {
let tampil = cookies.janganTampil === 'true';
if (tampil) {
setIsCookieSet(true);
}
}
return (
<div onLoad={checkCookie} className="App">
...
<Modal isCookieSet={isCookieSet} setIsCookieSet={setIsCookieSet} cookies={cookies} setCookie={setCookie} removeCookie={removeCookie} ></Modal>
</div>
);
模態
return createPortal(
<div className="modal fade" id="modal" tabIndex="-1" aria-labelledby="modalLabel" aria-hidden="true">
...
</div>, document.getElementById('popup')
)
show() function 需要在模態實例上使用。 所以在你之后:
import { Modal } from "bootstrap";
你可以做類似的事情:
const openModal = () => {
// There are other functions that might be more suitable
// for your situation than getOrCreateInstance()
// so I suggest you look into that.
const modalInstance = Modal.getOrCreateInstance('#modal');
modalInstance.show();
}
你究竟是如何實現的,如果不猜測你的代碼的 rest 是什么樣子,我就無法回答。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.