I’m developing a React application.
I’ve implemented an input field where users can enter values. However, I’m facing an issue with this input field. When users type, it displays intermediate values, such as "c," and "co," before showing the complete input, for example, "cow."
The reason I’ve stored the input as an array is that I have multiple instances of TextField generated in a loop, and I need to capture all the input values.
Here’s the relevant code snippet:
<TextField
id="standard-basic"
label="Standard"
variant="standard"
value={inputValue}
onChange={(e) => {
handleInputValue(e.target.value);
}}
/>
const [inputValue, setInputValue] = useState([]);
const handleInputValue = (inputSubmitted) => {
setInputValue([...inputValue, inputSubmitted]);
};
My goal is to store multiple input values from different TextFields while ensuring that the input field displays only the final complete value, avoiding the display of intermediate values.
How can I achieve this behaviour? to handle multiple input values in a loop?
2
Answers
I have an idea for your problem.
Try using the useRef hook to store and update the array every time an onKeyDown event is received like the sample below:
useRef is a way to help you store values away from being re-rendered by the component.
Sorry if my English is extremely bad.
If you’re trying to hold multiple states without overlap you can try a Map instead of an Array.