简体   繁体   English

我无法更改 React 钩子中的输入值

[英]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.

相关问题 酶模拟更改输入不会更改 React Hooks 上的值 - Enzyme simulate change input don't change value on React Hooks 为什么我不能更改 React 中的输入字段值? - Why can't I change the input field value in React? 为什么我不能在 React 中更改我的输入值? - Why can't I change my input value in React? 我无法使用 React 钩子更新我的字符串值 - I can't update my string value with React hooks React Hooks:输入组件(Semantic-ui-react)的值在重新渲染后不会改变 - React Hooks: Value of Input component (Semantic-ui-react) doesn't change after re-render React 检测到 Hooks 的调用顺序发生了变化,但我看不到任何有条件调用的 hooks - React has detected a change in the order of Hooks called, but I can't see any hooks being called conditonally React-hooks。 如何使输入显示并在 setstate 值后获得输入的焦点?谢谢 - React-hooks. How can I make the input display and get the focus of the input after the setstate value?Thanks 为什么我需要使用对象解构来使用React Hooks正确更改受控输入的值? - Why I need to use object desctructuring to properly change value of controlled input with React Hooks? React我无法更改输入字段的值(如果它具有值)和onChange - React I can't change my input field's value if it has a value and onChange 为什么即使使用 onChange 监听器我也不能在 React 中更改我的输入值 - Why can't I change my input value in React even with the onChange listener
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM