I have components where In each I have one form on click of next I am opening a new form( calling new component) and when it is last I am changing it to submit.
What I want to achieve
1: on each next and back click I want to validate my form using react-form-hook
2: on click of submit I should able to see all the data
3: while clicking on next to back data should not loss from inputs
Issue
So for validation I am using react-hook-form as it is very simple to use, but here I am not able to find out as my buttons are not inside the forms they are in main component so How will I validate and on submit the form on submit click wit all data.
My code
import React, { useState } from "react";
import Form1 from "./components/Form1";
import Form2 from "./components/Form2";
import Form3 from "./components/Form3";
function AddCompanyMain() {
const [currState, setCurrState] = useState(1);
const [allState, setAllstate] = useState(3);
const moveToPrevious = () => {
setCurrState(currState - 1);
};
const moveToNext = () => {
setCurrState(currState + 1);
};
return (
<div>
<div class="progress">
<div>{currState}</div>
</div>
{currState === 1 && <Form1 />}
{currState === 2 && <Form2 />}
{currState === 3 && <Form3 />}
{currState !== 1 && (
<button
className="btn btn-primary"
type="button"
onClick={moveToPrevious}
>
back
</button>
)}
{currState !== allState && (
<button className="btn btn-primary" type="button" onClick={moveToNext}>
next
</button>
)}
{currState === 3 && (
<button className="btn btn-primary" type="submit">
Submit
</button>
)}
</div>
);
}
export default AddCompanyMain;
My Full code Code sandbox
I am just looking for a good approach because here I can't put submit in each component, because I have to show steps at the top.