0

I am not able to enter more than one character in the input type="text" element in react.

After first character, the react app is rerendering preventing me to enter another character.Please let me know what i am missing here.

CODESANDBOX

4
  • 2
    Please add a minimal example of what you're trying to achieve or a code example that shows what you are doing. Commented Nov 3, 2020 at 17:50
  • Updated the sandbox with minimalistic code. All I want is to enter data with multiple characters in the input field. It's letting me enter only one character. Commented Nov 3, 2020 at 18:02
  • check onChange={onChangeQuantityHandler} in productview.js... expanding something so simple into something so elaborate and wide.... that's all i could tell u(im hungry) Commented Nov 3, 2020 at 18:05
  • I have an array of input fields that is the major problem . If i want to update only one input field , its straightforward just e.target.value in the state variable and use that state variable as the value for input field. I need to update the array of object which is productData state. Once i update it, it rerenders with the update value on in the input field where i added the text. Commented Nov 3, 2020 at 18:09

1 Answer 1

2

The issue is with your key value, which you are setting to uuid(). Every time the component re-renders you generate a new UUID, which creates a brand new input. You can see it working here using the index as the key instead.

import React, { useState, useEffect } from "react";
import { InnerWrapper, Heading } from "./styling";
import uuid from "react-uuid";

export const ProductView = (props) => {
  const [productData, setProductData] = useState(props.productData);

  const updateProductData = (newQuantity = undefined, index = "") => {
    const _ = productData.map((data, idx) => {
      if (idx === index) {
        return {
          ...data,
          quantity: newQuantity ? newQuantity : data.quantity
        };
      } else {
        return { ...data };
      }
    });
    setProductData(_);
  };

  const onChangeQuantityHandler = (e) => {
    e.preventDefault();
    const newQuantity = parseInt(e.target.value.trim());
    const index = parseInt(e.target.dataset.index);
    updateProductData(newQuantity, index);
  };

  return (
    <InnerWrapper flexDirection="column">
      <table>
        <thead>
          <tr>
            <th> Quantity </th>
          </tr>
        </thead>
        <tbody>
          {productData.map((data, idx) => {
            return (
              <tr key={idx}>
                <td>
                  <input
                    value={data.quantity}
                    name="quantity"
                    data-index={idx}
                    onChange={onChangeQuantityHandler}
                  />
                </td>
              </tr>
            );
          })}
        </tbody>
      </table>
    </InnerWrapper>
  );
};

If you truly want to use a UUID as the key, I suggest you generate one beforehand.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank You so much it worked.. My understanding was we should never use array index as the key.
There is nothing wrong with using the array index as the key, 99% of the time that is what you want. However, if you add/remove/re-order your components then you can get into trouble doing this. Check out the docs for more information: reactjs.org/docs/lists-and-keys.html#keys
Got it.. Btw it's also not letting enter the value of 0 in any of the input.
I updated the example in the codesandbox. It should allow 0 and empty. It is a simplified version: codesandbox.io/s/epic-fire-c4if8?file=/src/ProductView.js
Thanks again. Works like a charm now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.