reactjs 在React上创建多个输入

but5z9lq  于 2023-01-02  发布在  React
关注(0)|答案(1)|浏览(168)

Home Component
Data-Form Component
useInput Component
Table ComponentOutput of Code (I can't add 3. row)
我想得到输入,并把它作为表数据表。但我只能添加一个三元组数据,我不能添加第二行。第二个问题是,我想重置输入框,当我提交的形式,我怎么能做到这一点。第三个问题是,我怎么能使空表在开始(我无法描述空对象).我不需要保存在Json或任何数据库中,我只是想在打开网站时添加它.我的意思是它将是临时数据.谢谢你的帮助:)

eivnm1vs

eivnm1vs1#

var inputData = []  //Global

const DataFormComponent = () => {
  const [formData, setFormData] = useState({data1: "", data2: "", data3:""})
  const handleSubmit = (event) => {
         event.preventDefault();
         setFormData({...formData, data1, data2, data3})  //spread operator and also duplicate properties in object gets assigned the new value         
         inputData.push(formData);
         console.log(inputData);
         setFormData({data1: "", data2: "", data3: ""}
  }

  return(
     <form onSubmit = {handleSubmit}>
        <input name = "data1" type = "txt"/>
        <input name = "data2" type = "txt"/>
        <input name = "data3" type = "txt"/>
        <button type = "submit">SUBMIT FORM</button>
      </form>
    )
  }

export default DataFormComponent

相关问题