How to validate multistep form in react

后端 未结 2 542
时光取名叫无心
时光取名叫无心 2021-01-03 21:38

I have a multi-step form, which I am making using react, to work on validation I am using react-hook-form.

I have already achieved 90% of things just one

2条回答
  •  鱼传尺愫
    2021-01-03 22:19

    You can create an array of 2 empty strings where first one represents first row F name,and second one represents first row S Name under yours inputHolder in the MainComponent:

     const [inputHolder, setinputHolder] = useState([
        {
          id: 1,
          fname: "",
          sname: ""
        }
      ]);
      const [names, setNames] = useState(["", ""]);
    

    Next,every time the user ads a new row you add to empty strings in names array.

     const addRow = () => {
        console.log(inputHolder.length);
        let item = {
          id: inputHolder.length + 1,
          fname: "",
          sname: ""
        };
        setinputHolder([...inputHolder, item]);
        setNames([...names, "", ""]);
       
      };
    

    Where each empty string represents F name and S name. Finally,fields is set to names in second form:

    fields: names,
          component: (register, errors, defaultValues) => (
            
    

提交回复
热议问题