1

What i am trying to achieve is that i want to allow users to upload as many or limited files. To add he can always add the "Input file field".

Everything works correctly but when i try to delete it. It does not delete to "Input field" of SPECIDIC INDEX or Specific ID provided but it deletes the last field always.

import React, { useState } from "react";
import ReactDOM from "react-dom";

import "./styles.css";

function App() {
  const [fields, setFields] = useState([{ files: null }]);

  function handleChange(i, event) {
    const values = [...fields];
    values[i].files = event.target.[0];
    console.log(values);

    setFields(values);
  }

  function handleAdd() {
    const values = [...fields];
    values.push({ value: null });
    setFields(values);
  }

  function handleRemove(i) {
    const values = [...fields];
    values.splice(i, 1);
    setFields(values);
  }

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>

      <button type="button" onClick={() => handleAdd()}>
        +
      </button>

      {fields.map((field, idx) => {
        return (
          <div key={`${field}-${idx}`}>
            <input
              type="text"
              
               defaultValue={field.files || ""}
               onChange={(e) => handleChange(idx, e)}
                />
            <button type="button" onClick={() => handleRemove(idx)}>
              X
            </button>
          </div>
        );
      })}
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

1 Answer 1

1

The problem is the map KEY you should use a specific id instead of the index or the file object you can find below the example with the uuid package.

EXAMPLE :

import { useState } from "react";
import "./styles.css";
import { v4 as uuidv4 } from "uuid";

const App = () => {
 const [fields, setFields] = useState([{ id: uuidv4(), files: "" }]);

   function handleChange(i, event) {
     setFields(
      fields.map((input) =>
    input.id === i ? { ...input, files: event.target.value } : input
    )
    );
  }

  function handleAdd() {
  const values = [...fields];
  values.push({ id: uuidv4(), files: "" });
  setFields(values);
  }

  function handleRemove(idx) {
  setFields(fields.filter((_, i) => i !== idx));
  }

  return (
   <div className="App">
   <h1>Hello CodeSandbox</h1>

   <button type="button" onClick={() => handleAdd()}>
    +
   </button>

   {fields.map((field, idx) => {
    return (
      <div key={field.id}>
        <input
          type="text"
          value={field.files}
          onChange={(e) => handleChange(field.id, e)}
        />
        <button type="button" onClick={() => handleRemove(idx)}>
          X
        </button>
      </div>
    );
  })}
</div>
 );
 };
 export default App;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot that worked with the flow... :)
Glad to help :)

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.