【发布时间】:2019-10-17 23:53:58
【问题描述】:
我试图为这个特定问题找到解决方案,但我可以找到它......在新的这个,我有这个代码:
import React, { Fragment, useState } from 'react'
import '../../media/style/modal.css'
export default function Modal(props) {
const { activateModal } = props
const[values, setValues]= useState({name:'',email:''})
if (activateModal) {
document.getElementById('modals').click()
}
function handleValues(){
setValues({
name:document.getElementById('name').value,
email:document.getElementById('email').value
})
}
return (
<Fragment>
<button type="button" id="modals" style={{ display: 'none' }} data-toggle="modal" data-target="#exampleModalCenter">
Launch demo modal
</button>
<div className="modal fade" id="exampleModalCenter" tabIndex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div className="modal-dialog modal-dialog-centered" role="document">
<div className="modal-content">
<form onSubmit={handleSubmit}>
<div className="container">
<div className="row">
<button type="button" className="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div className="row justify-content-center">
<h5 className="modal-title" id="modalTitle"><span style={{color:'rgb(197,115,199)'}}>get- </span>started</h5>
</div>
<div className="row justify-content-center">
<input type="text" id="name" name="name" onChange={handleValues} />
<input type="email" id="email" name="email" />
</div>
<div className="row justify-content-center">
<button type="submit" id="btn2">Send</button>
</div>
</div>
</form>
</div>
</div>
</div>
</Fragment>
)
}
当我在输入中写一些东西时,我的页面重新呈现...我不知道如何停止这种行为,知道吗?...我尝试使用 useRef 但我不知道我是否正确使用它所以我没有得到想要的效果
【问题讨论】:
-
你应该发布你的整个文件
-
可能是时候将您的示例应用程序放在显示此行为的React Playground 上:当我在输入中写一些东西时,我的页面会重新呈现
标签: javascript reactjs jsx react-hooks