10

I have two Question ->

First one is I want to add user input field dynamically when user click "+" button in react.js. And if user click more times then more number of field add to the form. How to do this in react.js?

Second one when user change its value I want to store their corresponding value of each input field into my component state variable. Now here how I add their value, what data structure I use array or object or something else ? and how ?

1
  • 1
    Can you add some code that you have tried? Commented Mar 4, 2021 at 6:48

2 Answers 2

19
function App() {
  const inputArr = [
    {
      type: "text",
      id: 1,
      value: ""
    }
  ];

  const [arr, setArr] = useState(inputArr);

  const addInput = () => {
    setArr(s => {
      return [
        ...s,
        {
          type: "text",
          value: ""
        }
      ];
    });
  };

  const handleChange = e => {
    e.preventDefault();

    const index = e.target.id;
    setArr(s => {
      const newArr = s.slice();
      newArr[index].value = e.target.value;

      return newArr;
    });
  };

  return (
    <div>
      <button onClick={addInput}>+</button>
      {arr.map((item, i) => {
        return (
          <input
            onChange={handleChange}
            value={item.value}
            id={i}
            type={item.type}
            size="40"
          />
        );
      })}
    </div>
  );
}
Sign up to request clarification or add additional context in comments.

2 Comments

it works, but I also want to store the user input value of field in my state variable. How to do that ?
check, i update example. fast look - stackblitz.com/edit/react-vjfjzz?file=src/App.js
15

I would set a counter and increase it with each click of a button. I would map the counter numbers into an array and loop over creating new input elements.

import { useState } from "react";
import "./styles.css";

export default function App() {
  const [counter, setCounter] = useState(0);

  const handleClick = () => {
    setCounter(counter + 1);
    console.log(counter);
  };
  return (
    <div className="App">
      <button onClick={handleClick}>Hello</button>

      {Array.from(Array(counter)).map((c, index) => {
        return <input key={c} type="text"></input>;
      })}
    </div>
  );
}

https://codesandbox.io/s/elastic-wave-36ous?fontsize=14&hidenavigation=1&theme=dark

5 Comments

and how to store all input field value enter which was enter by the user to my state variable ?
@Rajatkashyap you could use an array to store all the values for your input fields. I will update my answer with that shortly
@Rajatkashyap I edited my code to store the values as well
it is adding input but i am facing one error here whenever i click on add it is reloading the page also. also i am not able to check the value in console. if function call is not working then how new input is added. if it working then value is not coming in console.
maybe i am not sure , but i thing my value is not setting in state variable but why. i only set is useState variable value is 1 because i need one input then new we can add but it reloading the page because of this i can add one input after a sec pafe reloading.

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.