【发布时间】:2021-12-27 05:48:33
【问题描述】:
我正在用 HTML、CSS 和纯 JS 制作网页。我打算把它转换成 ReactJS。虽然,我打算将它制作成一个 JSX 文件以将其导出到 App.js。我的朋友推荐我使用 Styled Components,这样我就可以将所有这些组件整合到一个 JSX 文件中。
问题是,我有一个按钮,单击时会显示一个模态框,使用纯 JS 中的函数。目前,我无法在 ReactJS 上实现该功能。
我在 React 中的代码看起来有点像这样:
//The Exported page
import React, {useState} from "react";
import styled from "styled-components";
export function InfoCalon(){
return(
<CustomStyle>
<div class="content-container">
<button id="button1" class="button" >Button 1</button>
</div>
<div id="modal" class="modalpopup">
<div class="modal-content">
//Modal Contents
</div>
</div>
</CustomStyle>
)
}
const CustomStyle = styled.div`
background-color: black;
color: white;
.button {
border: none;
min-width: 30%;
height: auto;
display: inline-flexbox;
text-align: center;
text-decoration: none;
margin: 10px 10% 10px 10%;
transition-duration: 0.2s;
cursor: pointer;
}
.button:hover{
transform: scale(1.02);
}
.button:active{
transform: scale(0.97);
transition-duration: 0.05s;
}
.modalpopup {
display: none; //The attribute that I want to change on button1 click to display: block.
position: fixed;
z-index: 10;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.4);
}
.modal-content {
background-color: #102037;
margin: 7% auto;
position: relative;
padding: 1% 3% 3% 3%;
width: 80%;
}
`
我尝试在按钮中使用 onClick 并创建了一个显示模式的函数,但我总是失败,出现诸如“var is null()”之类的错误。
在转换为ReactJS之前,JS文件中的代码是:
var btn1 = document.getElementById("button1");
btn1.onclick = function() {
modal.style.display = "block";
}
对于 HTML 和 CSS 代码,它几乎是 CustomStyle 中的所有内容。
如果您对此有任何回应,我将不胜感激,谢谢。
【问题讨论】:
标签: javascript html css reactjs styled-components