获取Onchange功能的最后值
//let's say these are the 3 variables i got from the text
const varArray = ['montant', 'test','date']
//and here is where i want to store the name of the variable and the value the user typed in the
//text field
const [variableValues, setVariableValues] = useState([{name: '', value: ''}]);
//this is the what i m doing in order to solve the given problem
{
varArrayCopy.map(
variable =>
<TextField
key={variable}
margin="dense"
label={variable}
fullWidth
variant="standard"
onChange={e => setVariableValues([...variableValues, {name: variable, value: e.target.value}])}
/>
)
}
即时通知将数据发送回警报中(我单击“发送”按钮之后),结果我得到的是
//let's say these are the 3 variables i got from the text
const varArray = ['montant', 'test','date']
//and here is where i want to store the name of the variable and the value the user typed in the
//text field
const [variableValues, setVariableValues] = useState([{name: '', value: ''}]);
//this is the what i m doing in order to solve the given problem
{
varArrayCopy.map(
variable =>
<TextField
key={variable}
margin="dense"
label={variable}
fullWidth
variant="standard"
onChange={e => setVariableValues([...variableValues, {name: variable, value: e.target.value}])}
/>
)
}
i m sending back the data in an alert (after i click on the "send" button) and the result i m getting is this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来这样,因为您使用了 Onchange Prop。每种击键都会更改您的变量值状态,从而通过字母添加了一个对象。您可以做的是将 useref 用于文本字段以获取值。您可以创建一个名为InputRefs的数组,该数组将存储这些参考文献&amp;只需循环穿过它们的vararraycopy.map
最后,删除Onchange变量,然后将 setVariable值添加到发送按钮函数。也像重构一样
这将在文本框中获取当前值。希望这对兄弟有帮助。请参阅文档: https://reactjs.s.s.org/docs/hooks-reference-refers-reaks-reference-reference.html#html#-html#- useref
It appears like that because you used onChange prop. Each keystroke renders a change to your variableValues state thus adding a object letter by letter. What you can do is use the useRef hook for your Text fields in order to get the values. You could create an array named inputRefs which would store these refs & just loop through them through your varArrayCopy.map
Lastly, remove the onChange variable and add the setVariableValues to your Send button function. Also refactor it like
This will get the current value inside the text box. Hope this would help brother. Refer to docs: https://reactjs.org/docs/hooks-reference.html#useref