获取Onchange功能的最后值

发布于 2025-01-19 12:42:47 字数 1014 浏览 3 评论 0原文

//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

The data i m getting back

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何处潇湘 2025-01-26 12:42:47

看起来这样,因为您使用了 Onchange Prop。每种击键都会更改您的变量值状态,从而通过字母添加了一个对象。您可以做的是将 useref 用于文本字段以获取值。您可以创建一个名为InputRefs的数组,该数组将存储这些参考文献&amp;只需循环穿过它们的vararraycopy.map

const montant = useRef(null);
const test = useRef(null);
const date = useRef(null);

const inputRefs = [montant, test, date]

{
   varArrayCopy.map(
        (variable, index) =>
              <TextField
                    key={variable}
                    margin="dense"
                    label={variable}
                    fullWidth
                    variant="standard"
                    ref={inputRefs[index]}
                    onChange={e => setVariableValues([...variableValues, {name: variable, value: inputRefs[index].current}])}
               />
   )
}

最后,删除Onchange变量,然后将 setVariable值添加到发送按钮函数。也像重构一样

setVariableValues([...variableValues, {name: variable,value:inputRefs[index].current}

这将在文本框中获取当前值。希望这对兄弟有帮助。请参阅文档: 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

const montant = useRef(null);
const test = useRef(null);
const date = useRef(null);

const inputRefs = [montant, test, date]

{
   varArrayCopy.map(
        (variable, index) =>
              <TextField
                    key={variable}
                    margin="dense"
                    label={variable}
                    fullWidth
                    variant="standard"
                    ref={inputRefs[index]}
                    onChange={e => setVariableValues([...variableValues, {name: variable, value: inputRefs[index].current}])}
               />
   )
}

Lastly, remove the onChange variable and add the setVariableValues to your Send button function. Also refactor it like

setVariableValues([...variableValues, {name: variable,value:inputRefs[index].current}

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

独守阴晴ぅ圆缺 2025-01-26 12:42:47
    To avoid multiple calls to a function with the onChange property in a 
    React input, debounce or throttle can be used to limit the number of 
    calls to the function.

    In this example, the function handleInputChange will be executed after 
    500 milliseconds after the text input stops. This ensures that the 
    function is executed only once after the text input has stopped.

    import React, { useState } from 'react';
    import { debounce } from 'lodash';

    const MyComponent = () => {
    const [inputValue, setInputValue] = useState('');

    const handleInputChange = debounce((value) => {
    // aquí va la lógica que se ejecutará después del tiempo establecido
    }, 500);

    return (
    <input
      type="text"
      value={inputValue}
      onChange={(event) => {
        setInputValue(event.target.value);
        handleInputChange(event.target.value);
          }}
        />
      );
    };

    export default MyComponent;

    To avoid multiple calls to a function with the onChange property in a 
    React input, debounce or throttle can be used to limit the number of 
    calls to the function.

    In this example, the function handleInputChange will be executed after 
    500 milliseconds after the text input stops. This ensures that the 
    function is executed only once after the text input has stopped.

    import React, { useState } from 'react';
    import { debounce } from 'lodash';

    const MyComponent = () => {
    const [inputValue, setInputValue] = useState('');

    const handleInputChange = debounce((value) => {
    // aquí va la lógica que se ejecutará después del tiempo establecido
    }, 500);

    return (
    <input
      type="text"
      value={inputValue}
      onChange={(event) => {
        setInputValue(event.target.value);
        handleInputChange(event.target.value);
          }}
        />
      );
    };

    export default MyComponent;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文