【发布时间】:2021-06-18 12:59:17
【问题描述】:
我的反应功能组件中有 4 个输入字段, 我做了一个函数contactMeButtonPressed,我想抓取所有的输入 当我点击联系我按钮时,姓名、电子邮件、公司名称、消息。
我是否需要在这里设置一个状态,或者我可以通过我的代码实现我的目标?
- 现在我正在控制台上打印以进行测试,稍后我会将信息发送到我的 Django 后端。
contact.js
import React, {Component} from 'react';
import Navbar from "./Navbar";
import {useStyles} from '../../static/styles/styles';
import {TextField, Typography, Button, Grid, Box, withStyles} from "@material-ui/core";
import SendIcon from "@material-ui/icons/Send";
import {WithStyles, makeStyles} from "@material-ui/core/styles";
const InputField = withStyles({
root: {
"& label.Mui-focused": {
color: "tomato",
},
"& label": {
color: "tan",
},
"& .MuiOutlinedInput-root": {
"& fieldset": {
borderColor: "tan",
},
"&:hover fieldset": {
borderColor: "tan"
},
"&.Mui-focused fieldset": {
borderColor: "tan",
}
}
},
})(TextField);
function contactMeButtonPressed() {
const requestOptions = {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({
// how to get all the InputField from box component in Contact
}),
};
fetch("/backend/contact-me/", requestOptions)
.then((response) => response.json())
.then((data) => console.log(requestOptions));
}
const Contact = () => {
const classes = useStyles(); // To use the CSS Styles in styles.js
return (
<>
<Box component="div" style={{background: "#233", height: "100vh"}}>
<Navbar/>
<Grid container justify="center">
<Box component="form" className={classes.form}>
<Typography variant="h5" style={{
color: "tomato",
textAlign: "center",
textTransform: "uppercase",
}}
>
Contact me
</Typography>
<InputField
fullWidth={true}
label="Name"
variant="outlined"
inputProps={{style: {color: "white"}}}
margin="dense"
size="medium"
/>
<br/>
<InputField
fullWidth={true}
label="Email"
variant="outlined"
inputProps={{style: {color: "white"}}}
margin="dense"
size="medium"
/>
<br/>
<InputField
fullWidth={true}
label="Company Name"
variant="outlined"
inputProps={{style: {color: "white"}}}
margin="dense"
size="medium"
/>
<br/>
<InputField
fullWidth={true}
label="Message"
variant="outlined"
inputProps={{style: {color: "white"}}}
margin="dense"
size="medium"
/>
<br/>
<Button
className={classes.contactButton}
variant="outlined"
fullWidth={true}
endIcon={<SendIcon/>}
onClick={contactMeButtonPressed}
>
Contact me
</Button>
</Box>
</Grid>
</Box>
</>
);
};
export default Contact;
使用 Hooks 的解决方案代码:
import React, {Component} from 'react';
import Navbar from "./Navbar";
import {useStyles} from '../../static/styles/styles';
import {TextField, Typography, Button, Grid, Box, withStyles} from "@material-ui/core";
import SendIcon from "@material-ui/icons/Send";
import {WithStyles, makeStyles} from "@material-ui/core/styles";
import {useState} from 'react';
const InputField = withStyles({
root: {
"& label.Mui-focused": {
color: "tomato",
},
"& label": {
color: "tan",
},
"& .MuiOutlinedInput-root": {
"& fieldset": {
borderColor: "tan",
},
"&:hover fieldset": {
borderColor: "tan"
},
"&.Mui-focused fieldset": {
borderColor: "tan",
}
}
},
})(TextField);
const Contact = () => {
const [name, setName] = useState('')
const [email, setEmail] = useState('')
const [company_name, setCompany_name] = useState('')
const [message, setMessage] = useState('')
function contactMeButtonPressed() {
const requestOptions = {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({
name: name,
company_name: company_name,
message: message,
email: email,
}),
};
fetch("/backend/contact-me/", requestOptions)
.then((response) => response.json())
.then((data) => console.log(requestOptions));
}
const classes = useStyles(); // To use the CSS Styles in styles.js
return (
<>
<Box component="div" style={{background: "#233", height: "100vh"}}>
<Navbar/>
<Grid container justify="center">
<Box component="form" className={classes.form}>
<Typography variant="h5" style={{
color: "tomato",
textAlign: "center",
textTransform: "uppercase",
}}
>
Contact me
</Typography>
<InputField
value={name}
onChange={e => setName(e.target.value)}
fullWidth={true}
label="Name"
variant="outlined"
inputProps={{style: {color: "white"}}}
margin="dense"
size="medium"
/>
<br/>
<InputField
value={email}
onChange={e => setEmail(e.target.value)}
fullWidth={true}
label="Email"
variant="outlined"
inputProps={{style: {color: "white"}}}
margin="dense"
size="medium"
/>
<br/>
<InputField
value={company_name}
onChange={e => setCompany_name(e.target.value)}
fullWidth={true}
label="Company Name"
variant="outlined"
inputProps={{style: {color: "white"}}}
margin="dense"
size="medium"
/>
<br/>
<InputField
value={message}
onChange={e => setMessage(e.target.value)}
fullWidth={true}
label="Message"
variant="outlined"
inputProps={{style: {color: "white"}}}
margin="dense"
size="medium"
/>
<br/>
<Button
className={classes.contactButton}
variant="outlined"
fullWidth={true}
endIcon={<SendIcon/>}
onClick={contactMeButtonPressed}
>
Contact me
</Button>
</Box>
</Grid>
</Box>
</>
);
};
export default Contact;
【问题讨论】:
标签: javascript reactjs