这里有表格数据。我必须将数据推送到数组中。同时,我必须检索该数据并将其显示在另一个组件中。
import React from 'react';
import Button from '@material-ui/core/Button';
import Dialog from '@material-ui/core/Dialog';
import DialogActions from '@material-ui/core/DialogActions';
import DialogContent from '@material-ui/core/DialogContent';
import DialogContentText from '@material-ui/core/DialogContentText';
import DialogTitle from '@material-ui/core/DialogTitle';
import TextField from '@material-ui/core/TextField';
import { Link } from 'gatsby';
import DisplayOutput from '../pages/DisplayOutput';
import { Formik, Field, Form, ErrorMessage } from 'formik';
import * as Yup from 'yup';
class DialogFormWithFormik extends React.Component {
constructor(props) {
super(props)
this.state = {
open: false,
userName: '',
password: '',
data:'',
dialogModal: 'none',
}
}
handleClickOpen = () => {
this.setState({ open: true })
this.setState({ dialogModal: 'login' })
}
handleRegisterClickOpen = () => {
this.setState({ open: true })
this.setState({ dialogModal: 'register' })
}
handleClose = () => {
this.setState({ dialogModal: false })
}
handleUsernameChange = event => {
this.setState({
userName: event.target.value,
})
}
handlePasswordChange = e => {
this.setState({
password: e.target.value,
})
}
handleSubmit = () => {
console.log(this.state.userName + '' + this.state.password)
alert('values submitted')
}
form = props => {
return (
<div>
<Button
variant="outlined"
color="primary"
onClick={() => this.handleClickOpen()}
>
Login
</Button>
<Button
variant="outlined"
color="primary"
onClick={() => this.handleRegisterClickOpen()}
>
Register
</Button>
<Dialog
onClose={() => this.handleClose()}
aria-labelledby="customized-dialog-title"
open={this.state.dialogModal === 'login'}
>
<Form onSubmit={this.handleSubmit}>
<DialogTitle id="form-dialog-title">
To Display Student Data
</DialogTitle>
<TextField
label="Username"
type="text"
margin="normal"
name="userName"
id="userName"
onChange={this.handleUsernameChange}
value={this.state.userName}
/>
<br />
<TextField
label="Password"
type="password"
autoComplete="current-password"
margin="normal"
id="passsword"
onChange={this.handlePasswordChange}
value={this.state.password}
/>
<br />
<DialogActions>
<nav>
<Button variant="contained" type="submit" color="primary">
Login
</Button>
</nav>
<br />
<Button onClick={() => this.handleClose()}>Cancel</Button>
</DialogActions>
</Form>
</Dialog>
</div>
)
}
schema = () => {
const schema = Yup.object().shape({
userName: Yup.string().required(),
password: Yup.string().required(),
})
return schema
}
render() {
return (
<div className="Sample" align="center">
<Formik render={this.form} />
</div>
)
}
}
export default DialogFormWithFormik
答案 0 :(得分:1)
在处理表单提交的方法中,将事件作为参数,然后调用preventDefault
Read more here。
因此,提交时页面不会重新加载。然后将数据推送到正确的数组。
handleSubmit = (event) => {
event.preventDefault();
...
}
答案 1 :(得分:0)
尝试一下。我仅为一个输入字段创建一个,然后根据您的项目文件导入和导出您的组件
[DateTimeResourceFilter]
[Route("{providerName}/CalendarAvailability")]
[HttpGet]
public Task<CalendarAvailabilityResponse> GetCalendarAvailability(CalendarAvailabilityRequest request)
{
return null;
}