【发布时间】:2019-06-21 00:29:50
【问题描述】:
我将以下代码用作教程:
CommentSend.js
import React, { useState } from "react";
import Dialog from "@material-ui/core/Dialog";
import FormControl from "@material-ui/core/FormControl";
import Button from "@material-ui/core/Button";
import Input from "@material-ui/core/Input";
import InputLabel from "@material-ui/core/InputLabel";
const CommentSend = () => {
const [description, setDescription] = useState("");
const [open, setOpen] = useState(false)
return (
<form>
<Button
onClick={() => setOpen(true)}
type="submit"
>
Add Comment
</Button>
<Dialog
open={open}
>
<FormControl fullWidth>
<InputLabel htmlFor="comment">Comment</InputLabel>
<Input
id="comment"
onChange={event => setDescription(event.target.value)}
/>
</FormControl>
</Dialog>
</form>
);}
export default CommentSend;
点击按钮后,对话框没有打开,而是页面刷新。我不确定这里发生了什么。
【问题讨论】:
-
您的按钮是
submit类型,这意味着当您单击它时表单将提交。表单提交,提交网页(又名页面刷新)。而是为按钮指定不同的类型(如<button type="button")。您也应该在表单元素上添加onSubmit
标签: reactjs material-ui