📜  使用 id 响应动态输入 - TypeScript 代码示例

📅  最后修改于: 2022-03-11 14:48:15.439000             🧑  作者: Mango

代码示例1
import React from 'react';
import { useState } from 'react';

function DynamicInput() {

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

      function createInputs() {
        return values.val.map((el, i) =>
          
); } 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 (
{createInputs()}
); } export default DynamicInput;