[英]I can't change input value in React hooks
I'm quite new to React and Hooks and trying to make contact form with hooks.我对 React 和 Hooks 很陌生,并试图用钩子制作联系表格。 I can't change my values in the field and can't type anything in the input and the onChange() is not firing.我无法更改字段中的值,也无法在输入中输入任何内容,并且 onChange() 未触发。 But there is no error on browser and console, so I can not figure it out.但是浏览器和控制台上没有错误,所以我无法弄清楚。 Do you know the reason?你知道原因吗?
Here is my code.这是我的代码。
import React , {useState} 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 DialogTitle from '@material-ui/core/DialogTitle';
import TextInput from './TextInput'
const FormDialog = props => {
const {
handleClose,
open,
} = props;
const [values, setValues] = useState({
name: "",
email: "",
description: ""
});
const handleChange = event => {
setValues({
...values,
[event.target.name]: event.target.value
});
};
return(
<Dialog
open={open}
onClose={handleClose}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<DialogTitle id="alert-dialog-title">Contact Form</DialogTitle>
<DialogContent>
<TextInput
label={“name} multiline={false} rows={1}
value={values.name} type={"text"} onChange={handleChange}
/>
<TextInput
label={“email”} multiline={false} rows={1}
value={values.email} type={"email"} onChange={handleChange}
/>
<TextInput
label={“more”} multiline={false} rows={5}
value={values.description} type={"text"} onChange={handleChange}
/>
</DialogContent>
<DialogActions>
<Button onClick={props.handleClose} color="primary">
cancel
</Button>
<Button onClick={submitForm} color="primary" autoFocus>
send
</Button>
</DialogActions>
</Dialog>
)
}
export default FormDialog
You have not defined name attribute on TextInput like below..您尚未在 TextInput 上定义名称属性,如下所示..
<TextInput
label={“name} multiline={false} rows={1} name="name"
value={values.name} type={"text"} onChange={handleChange}
/>
<TextInput
label={“email”} multiline={false} rows={1} name="email"
value={values.email} type={"email"} onChange={handleChange}
/>
You can't spread a variable.你不能传播一个变量。 That is, const
.也就是说, const
。 You're spreading the state of the input value inside the onChange
function: ...values
.您正在onChange
函数内传播输入值的状态: ...values
。 Here you're just firing the value associated with that particular input tag.在这里,您只是触发与该特定输入标签关联的值。 And as you want to change it dynamically, you're enclosing the e.target.name with the square brackets.当您想动态更改它时,您将 e.target.name 用方括号括起来。 And also you're not populating the state.而且你没有填充状态。 Your just making the changes once at a time.您只需一次进行一次更改。 The spread operator is used to make a copy of the previous state and then fill the array with new items.展开运算符用于复制先前状态,然后用新项目填充数组。 Here in the input tag, you don't do that.在输入标签中,您不要这样做。 You just change the values once.您只需更改一次值。 If you want to change, you erase and enter a new one.如果要更改,请擦除并输入新的。 That is the reason you don't have to spread the state.这就是您不必传播状态的原因。
So Re-write your code with:所以重新编写你的代码:
setValue({ [event.target.name] : event.target.value })
inside your onChange
function setValue({ [event.target.name] : event.target.value })
在你的onChange
函数中
Have a nice day.祝你今天过得愉快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.