如何使用 React 钩子创建处理输入对象 [英] How Can I create a Handle an input object with React hooks
本文介绍了如何使用 React 钩子创建处理输入对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的输入对象有问题如何更改 onChage 函数,这是我的输入代码
<预><代码><Form.Group as={Col} controlId=formGridResultat"><Form.Label>结果</Form.Label><Form.Control required as="select";类型=文本"id=conduexam.id"名称=conduexam.id"值={当前状态.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}</选项>))}</Form.Control></Form.Group>函数onChange"如何更改?
<预><代码>const handleInputChange = 事件 =>{const { name, value } = event.target;setCurrentStates({ ...currentStates, [name]: value });}; 解决方案
试试这个:
const [currentStates, setCurrentStates]= useState({//你的初始代码在这里})const handleInputChange = 事件 =>{控制台.日志(事件.目标.值)setCurrentStates({ ...currentStates, [name]: event.target.value});};
I have problem in my input object how can I change the onChage function this is my input code
<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>
The function "onChange" how can I change it?
const handleInputChange = event => {
const { name, value } = event.target;
setCurrentStates({ ...currentStates, [name]: value });
};
解决方案
Try this:
const [currentStates, setCurrentStates]= useState({
//your initial code here
})
const handleInputChange = event => {
console.log(event.target.value)
setCurrentStates({ ...currentStates, [name]: event.target.value});
};
这篇关于如何使用 React 钩子创建处理输入对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文