【问题标题】:How Can I create a Handle an input object with React hooks如何使用 React 钩子创建句柄输入对象
【发布时间】:2021-07-01 19:48:50
【问题描述】:

我的输入对象有问题如何更改 onChage 函数这是我的输入代码



<Form.Group as={Col} controlId="formGridResultat">
                                                            <Form.Label>Result</Form.Label>
                                                            <Form.Control required as="select"
                                                                type="text"
                                                                id="conduexam.id"
                                                                name="conduexam.id"
                                                                value={currentStates. conduexam?.nom}
                                                                className={"bg-white text-dark"}
                                                                onChange={handleInputChange}>

                                                                <option value="">Choisir un Resultat </option>
                                                                {result.map((value) => (
                                                                    <option value={value.id} key={value.id}>
                                                                        {value.nom}
                                                                    </option>
                                                                ))}

                                                            </Form.Control>

                                                        </Form.Group>


“onChange”函数如何更改?


const handleInputChange = event => {
        const { name, value } = event.target;
        setCurrentStates({ ...currentStates, [name]: value });
    };

【问题讨论】:

  • “我怎样才能改变它?” ...改变它如何?你想完成什么?
  • @Lili result 数组长什么样子?

标签: reactjs react-hooks


【解决方案1】:

试试这个:

const [currentStates, setCurrentStates]= useState({
//your initial code here
})

const handleInputChange = event => {
        console.log(event.target.value)
        setCurrentStates({ ...currentStates, [name]: event.target.value});
   };

【讨论】:

  • 不能工作?它让我的 [name] 出错
  • 您需要在表单中检查您的value
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-12
  • 1970-01-01
相关资源
最近更新 更多