Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

react dynamic inputs with id

import React from 'react';
import { useState } from 'react';

function DynamicInput() {

    const [values, setValues] = useState({ val: []});

      function createInputs() {
        return values.val.map((el, i) =>
          <div key={i}>
            <input type="text" value={el||''} onChange={handleChange.bind(i)} />
            <input type='button' value='remove' onClick={removeClick.bind(i)} />
          </div>
        );
      }

    function handleChange(event) {
      let vals = [...values.val];
      vals[this] = event.target.value;
      setValues({ val: vals });
    }

    const addClick = () => {
      setValues({ val: [...values.val, '']})
    }

    const removeClick = () => {
      let vals = [...values.val];
      vals.splice(this,1);
      setValues({ val: vals });
    }

    const handleSubmit = event => {
      alert('A name was submitted: ' + values.val.join(', '));
      event.preventDefault();
    }

    return (
      <form onSubmit={handleSubmit}>
          {createInputs()}
          <input type='button' value='add more' onClick={addClick} />
          <input type="submit" value="Submit" />
      </form>
    );

}

export default DynamicInput;
Comment

PREVIOUS NEXT
Code Example
Typescript :: jwt.verify into promise mongoose with typescript 
Typescript :: setup react with serverless stack 
Typescript :: typescript Erased Structural Types 
Typescript :: concat to String structs rust 
Typescript :: ts number addition is concatenating like strings 
Typescript :: Convert given seconds to space age on all planets of our solar system 
Typescript :: how to disable piecelabel on certian charts and keep on other chartjs 
Typescript :: Job for pm2-rfb.service failed because the service did not take the steps required by its unit configuration. 
Typescript :: React Draft Wysiwyg typescript 
Typescript :: hashMap.put("name", fruits Names[i]); 
Typescript :: Copy the first two array elements to the last two array elements 
Typescript :: ht office 
Typescript :: config all requests to one page nginx 
Typescript :: bts assurance 
Typescript :: vi highlights word 
Typescript :: css proferties throught ts 
Typescript :: weights [0.03333567, 0.07472567, 0.10954318, 0.13463336, 0.14776211, 0.14776211, 0.13463336, 0.10954318, 0.07472567, 0.03333567] 
Typescript :: error NG6002: Appears in the NgModule.imports of DashboardModule, but could not be resolved to an NgModule class. 
Typescript :: how to append different lists in installed apps django 
Typescript :: js Validating nested promises 
Typescript :: useScreenWidth 
Typescript :: Multiselect and Search in angular 13 
Typescript :: calculate fps html canvas 
Typescript :: check if element exists in array java 
Typescript :: ioredis 
Typescript :: t sql if exists multiple conditions 
Typescript :: // running tests Your code should no longer have a p tag around the text asking what level ninja a user is. // tests completed category:423 
Cpp :: go read file to string 
Cpp :: how to print numbers with only 2 digits after decimal point in c++ 
Cpp :: modify file cpp 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =