Reactjs 我能';t更改React HOOK中的输入值
我是一个新的反应和钩子,并试图使接触形式与钩子。 我无法更改字段中的值,也无法在输入中键入任何内容,并且onChange()未启动。但在浏览器和控制台上并没有错误,所以我无法找出它。 你知道原因吗 这是我的密码Reactjs 我能';t更改React HOOK中的输入值,reactjs,material-ui,react-hooks,Reactjs,Material Ui,React Hooks,我是一个新的反应和钩子,并试图使接触形式与钩子。 我无法更改字段中的值,也无法在输入中键入任何内容,并且onChange()未启动。但在浏览器和控制台上并没有错误,所以我无法找出它。 你知道原因吗 这是我的密码 import React , {useState} from 'react'; import Button from '@material-ui/core/Button'; import Dialog from '@material-ui/core/Dialog'; import Di
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
从“React”导入React,{useState};
从“@material ui/core/Button”导入按钮;
从“@material ui/core/Dialog”导入对话框;
从“@material ui/core/DialogActions”导入DialogActions;
从“@material ui/core/DialogContent”导入DialogContent;
从“@material ui/core/DialogTitle”导入DialogTitle;
从“./TextInput”导入TextInput
const FormDialog=props=>{
常数{
手镯,
打开
}=道具;
const[values,setValues]=useState({
姓名:“,
电邮:“,
说明:“”
});
const handleChange=事件=>{
设定值({
价值观
[event.target.name]:event.target.value
});
};
返回(
联系方式
取消
发送
)
}
导出默认窗体对话框
您还没有像下面这样在TextInput上定义name属性
<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}
/>
您还没有像下面这样在TextInput上定义name属性
<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}
/>
您不能传播变量。也就是说,
const
。您正在onChange
函数中扩展输入值的状态:…values
。在这里,您只是触发与特定输入标记关联的值。当您想要动态更改它时,您将用方括号括住e.target.name。而且你也没有在这个州居住。您只需一次做一次更改。spread运算符用于复制以前的状态,然后用新项填充数组。在这里的输入标记中,您不能这样做。只需更改一次值。如果要更改,请删除并输入一个新的。这就是你不必传播国家的原因
因此,请使用以下方法重新编写代码:
setValue({[event.target.name]:event.target.value})
在onChange
函数中
祝你度过愉快的一天。你不能传播一个变量。也就是说,
const
。您正在onChange
函数中扩展输入值的状态:…values
。在这里,您只是触发与特定输入标记关联的值。当您想要动态更改它时,您将用方括号括住e.target.name。而且你也没有在这个州居住。您只需一次做一次更改。spread运算符用于复制以前的状态,然后用新项填充数组。在这里的输入标记中,您不能这样做。只需更改一次值。如果要更改,请删除并输入一个新的。这就是你不必传播国家的原因
因此,请使用以下方法重新编写代码:
setValue({[event.target.name]:event.target.value})
在onChange
函数中
祝您愉快。因为没有
事件。target.name
绑定TextInput
您能为所有组件添加导入吗?因为我看不到材质UI中的TextInput,如果是您自己的,请也添加该代码。我使用的材质UI版本为“@material UI/core”:“^4.11.0”,“@material UI/icons”:“^4.9.1”,“@material ui/system”:“^4.9.14”,因为没有事件。target.name
bind inTextInput
您能为所有组件添加导入吗?因为我在material ui中看不到TextInput,如果它是您自己的,请也添加该代码。我使用的是这些版本的material ui“@material ui/core”:“^4.11.0”,“@material ui/icons”:“^4.9.1“,“@material ui/system”:“^4.9.14”,谢谢,我为每个TextInput添加了name属性,但仍然不起作用:(谢谢,我为每个TextInput添加了name属性,但仍然不起作用:(